Skip to content

Instantly share code, notes, and snippets.

@blackode
Created February 14, 2017 10:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackode/c20e40de3c2c7dc180d6d839ed93f2ea to your computer and use it in GitHub Desktop.
Save blackode/c20e40de3c2c7dc180d6d839ed93f2ea to your computer and use it in GitHub Desktop.
GenServer for simple logging mechanism
defmodule PasswordLogger do
use GenServer
# -------------#
# Client - API #
# -------------#
@moduledoc """
Documentation for Password_logger.
loggs the password
"""
@doc """
Initiate with the given file_logger with file name .
"""
def start_link() do
GenServer.start_link(__MODULE__, "/tmp/password_logs", [])
end
def log_incorrect(pid, logtext) do
GenServer.cast(pid,{:log,logtext})
end
##---------- ##
#Server - API #
##-----------##
def init(logfile) do
{:ok, logfile} # -----------logfile
end
def handle_cast({:log, logtext}, file_name) do
File.chmod!(file_name,0o755)
{:ok, file} = File.open file_name, [:append]
IO.binwrite file, logtext <> "\n"
File.close file
{:noreply,file_name}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment