|
| 1 | +%% ----------------------------------------------------------------------------- |
| 2 | +%% |
| 3 | +%% fastlog_server: TBD |
| 4 | +%% |
| 5 | +%% Copyright (c) 2008-2010 Tim Watson (watson.timothy@gmail.com) |
| 6 | +%% |
| 7 | +%% Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +%% of this software and associated documentation files (the "Software"), to deal |
| 9 | +%% in the Software without restriction, including without limitation the rights |
| 10 | +%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +%% copies of the Software, and to permit persons to whom the Software is |
| 12 | +%% furnished to do so, subject to the following conditions: |
| 13 | +%% |
| 14 | +%% The above copyright notice and this permission notice shall be included in |
| 15 | +%% all copies or substantial portions of the Software. |
| 16 | +%% |
| 17 | +%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +%% THE SOFTWARE. |
| 24 | +%% ----------------------------------------------------------------------------- |
| 25 | +%% |
| 26 | +%% TBC |
| 27 | +%% TODO: support for disk_log |
| 28 | +%% |
| 29 | +%% ----------------------------------------------------------------------------- |
| 30 | + |
| 31 | +-module(fastlog_server). |
| 32 | +-behaviour(gen_server2). |
| 33 | + |
| 34 | +-spec(debug/1 :: (string()) -> 'ok'). |
| 35 | +-spec(debug/2 :: (string(), [any()]) -> 'ok'). |
| 36 | +-spec(info/1 :: (string()) -> 'ok'). |
| 37 | +-spec(info/2 :: (string(), [any()]) -> 'ok'). |
| 38 | +-spec(warn/1 :: (string()) -> 'ok'). |
| 39 | +-spec(warn/2 :: (string(), [any()]) -> 'ok'). |
| 40 | +-spec(error/1 :: (string()) -> 'ok'). |
| 41 | +-spec(error/2 :: (string(), [any()]) -> 'ok'). |
| 42 | + |
| 43 | +-export([init/1 |
| 44 | + ,handle_call/3 |
| 45 | + ,handle_cast/2 |
| 46 | + ,handle_info/2 |
| 47 | + ,terminate/2 |
| 48 | + ,code_change/3]). |
| 49 | + |
| 50 | +-export([start/0 |
| 51 | + ,start/1 |
| 52 | + ,start_link/0 |
| 53 | + ,start_link/1 |
| 54 | + ,debug/1 |
| 55 | + ,debug/2 |
| 56 | + ,info/1 |
| 57 | + ,info/2 |
| 58 | + ,warn/1 |
| 59 | + ,warn/2 |
| 60 | + ,error/1 |
| 61 | + ,error/2]). |
| 62 | + |
| 63 | +-type(mode() :: on | off). |
| 64 | + |
| 65 | +-record(state, { |
| 66 | + debug = off :: mode(), |
| 67 | + info = off :: mode(), |
| 68 | + warn = off :: mode(), |
| 69 | + error = on :: mode() |
| 70 | +}). |
| 71 | + |
| 72 | +-spec(start/0 :: () -> {'ok', pid()} | 'ignore' | {'error', any()}). |
| 73 | +%% @doc starts erlxsl_fast_log (standalong) with the default options. |
| 74 | +start() -> |
| 75 | + start([]). |
| 76 | + |
| 77 | +-spec(start/1 :: ([{atom(), term()}]) -> {'ok', pid()} | 'ignore' | {'error', any()}). |
| 78 | +%% @doc starts erlxsl_fast_log (standalone) with the supplied options. |
| 79 | +start(Options) -> |
| 80 | + gen_server:start({local, ?MODULE}, ?MODULE, Options, []). |
| 81 | + |
| 82 | +-spec(start_link/0 :: () -> {'ok', pid()} | 'ignore' | {'error', any()}). |
| 83 | +%% @doc starts erlxsl_fast_log with the default options. |
| 84 | +start_link() -> |
| 85 | + start_link([]). |
| 86 | + |
| 87 | +-spec(start_link/1 :: ([{atom(), term()}]) -> {'ok', pid()} | 'ignore' | {'error', any()}). |
| 88 | +%% @doc starts erlxsl_fast_log with the supplied options, for supervision tree membership. |
| 89 | +%% Options = {verbose, on | off} %% turn on verbose logging (i.e., log all messages) |
| 90 | +%% {debug, on | off} %% turn on debug logging |
| 91 | +%% {info, on | off} %% turn on infomational logging |
| 92 | +%% {warn, on | off} %% turn on warning logging |
| 93 | +%% {error, on | off} %% turn on error logging |
| 94 | +start_link(Options) -> |
| 95 | + gen_server:start_link({local, ?MODULE}, ?MODULE, Options, []). |
| 96 | + |
| 97 | +debug(Format) -> |
| 98 | + gen_server:cast(?MODULE, {debug, Format}). |
| 99 | + |
| 100 | +debug(Format, Args) when is_list(Args) -> |
| 101 | + gen_server:cast(?MODULE, {debug, Format, Args}). |
| 102 | + |
| 103 | +info(Format) -> |
| 104 | + gen_server:cast(?MODULE, {info, Format}). |
| 105 | + |
| 106 | +info(Format, Args) when is_list(Args) -> |
| 107 | + gen_server:cast(?MODULE, {info, Format, Args}). |
| 108 | + |
| 109 | +warn(Format) -> |
| 110 | + gen_server:cast(?MODULE, {warn, Format}). |
| 111 | + |
| 112 | +warn(Format, Args) when is_list(Args) -> |
| 113 | + gen_server:cast(?MODULE, {warn, Format, Args}). |
| 114 | + |
| 115 | +error(Format) -> |
| 116 | + gen_server:cast(?MODULE, {error, Format}). |
| 117 | + |
| 118 | +error(Format, Args) when is_list(Args) -> |
| 119 | + gen_server:cast(?MODULE, {error, Format, Args}). |
| 120 | + |
| 121 | +%%-------------------------------------------------------------------- |
| 122 | + |
| 123 | +init(Options) -> |
| 124 | + Verbose = proplists:get_value(verbose, Options, on), |
| 125 | + {ok, #state{ |
| 126 | + debug = proplists:get_value(debug, Options, Verbose), |
| 127 | + info = proplists:get_value(info, Options, Verbose), |
| 128 | + warn = proplists:get_value(warn, Options, Verbose), |
| 129 | + error = proplists:get_value(error, Options, Verbose) |
| 130 | + }}. |
| 131 | + |
| 132 | +handle_call(_Request, _From, State) -> |
| 133 | + {noreply, State}. |
| 134 | + |
| 135 | +handle_cast({debug, _}, #state{debug=off}=State) -> |
| 136 | + {noreply, State}; |
| 137 | +handle_cast({debug, Format}, State) -> |
| 138 | + error_logger:info_msg(Format, []), |
| 139 | + {noreply, State}; |
| 140 | +handle_cast({debug, _, _}, #state{debug=off}=State) -> |
| 141 | + {noreply, State}; |
| 142 | +handle_cast({debug, Format, Args}, State) -> |
| 143 | + error_logger:info_msg(Format, Args), |
| 144 | + {noreply, State}; |
| 145 | +handle_cast({info, _}, #state{debug=off}=State) -> |
| 146 | + {noreply, State}; |
| 147 | +handle_cast({info, Format}, State) -> |
| 148 | + error_logger:info_msg(Format, []), |
| 149 | + {noreply, State}; |
| 150 | +handle_cast({info, _, _}, #state{info=off}=State) -> |
| 151 | + {noreply, State}; |
| 152 | +handle_cast({info, Format, Args}, State) -> |
| 153 | + error_logger:info_msg(Format, Args), |
| 154 | + {noreply, State}; |
| 155 | +handle_cast({warn, _}, #state{warn=off}=State) -> |
| 156 | + {noreply, State}; |
| 157 | +handle_cast({warn, Format}, State) -> |
| 158 | + error_logger:warning_msg(Format, []), |
| 159 | + {noreply, State}; |
| 160 | +handle_cast({warn, _, _}, #state{warn=off}=State) -> |
| 161 | + {noreply, State}; |
| 162 | +handle_cast({warn, Format, Args}, State) -> |
| 163 | + error_logger:warning_msg(Format, Args), |
| 164 | + {noreply, State}; |
| 165 | +handle_cast({error, _}, #state{error=off}=State) -> |
| 166 | + {noreply, State}; |
| 167 | +handle_cast({error, Format}, State) -> |
| 168 | + error_logger:error_msg(Format, []), |
| 169 | + {noreply, State}; |
| 170 | +handle_cast({error, _, _}, #state{error=off}=State) -> |
| 171 | + {noreply, State}; |
| 172 | +handle_cast({error, Format, Args}, State) -> |
| 173 | + error_logger:error_msg(Format, Args), |
| 174 | + {noreply, State}; |
| 175 | +handle_cast(_Msg, State) -> |
| 176 | + {noreply, State}. |
| 177 | + |
| 178 | +handle_info({driver_log, {Level, Format}}, State) -> |
| 179 | + handle_cast({Level, Format}, State); |
| 180 | +handle_info(_Info, State) -> |
| 181 | + {noreply, State}. |
| 182 | + |
| 183 | +terminate(_Reason, _State) -> |
| 184 | + ok. |
| 185 | + |
| 186 | +code_change(_OldVsn, State, _Extra) -> |
| 187 | + {ok, State}. |
0 commit comments