1 | ///////////////////////////////////////////////////////////////////////////// |
---|
2 | // Name: simplelog.c |
---|
3 | // Author: Cesar Mauri Loba (cesar at crea-si dot com) |
---|
4 | // Copyright: (C) 2012-13 Cesar Mauri Loba - CREA Software Systems |
---|
5 | // |
---|
6 | // This program is free software: you can redistribute it and/or modify |
---|
7 | // it under the terms of the GNU General Public License as published by |
---|
8 | // the Free Software Foundation, either version 3 of the License, or |
---|
9 | // (at your option) any later version. |
---|
10 | // |
---|
11 | // This program is distributed in the hope that it will be useful, |
---|
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | // GNU General Public License for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | ///////////////////////////////////////////////////////////////////////////// |
---|
19 | #include "simplelog.h" |
---|
20 | #include <assert.h> |
---|
21 | |
---|
22 | static slog_callback_t g_slog_callback= slog_stream_callback; |
---|
23 | static slog_priority_t g_slog_priority= SLOG_PRIO_ERR; |
---|
24 | |
---|
25 | SLOG_DECLSPEC const char* slog_get_priority_name (slog_priority_t prio) |
---|
26 | { |
---|
27 | switch(prio) { |
---|
28 | case SLOG_PRIO_EMERG: return "EMERGENCY"; |
---|
29 | case SLOG_PRIO_ALERT: return "ALERT"; |
---|
30 | case SLOG_PRIO_CRIT: return "CRITICAL"; |
---|
31 | case SLOG_PRIO_ERR: return "ERROR"; |
---|
32 | case SLOG_PRIO_WARNING: return "WARNING"; |
---|
33 | case SLOG_PRIO_NOTICE: return "NOTICE"; |
---|
34 | case SLOG_PRIO_INFO: return "INFO"; |
---|
35 | case SLOG_PRIO_DEBUG: return "DEBUG"; |
---|
36 | default: |
---|
37 | assert (0); |
---|
38 | } |
---|
39 | return NULL; |
---|
40 | } |
---|
41 | |
---|
42 | void slog_set_priority (slog_priority_t prio) |
---|
43 | { |
---|
44 | g_slog_priority= prio; |
---|
45 | } |
---|
46 | |
---|
47 | slog_priority_t slog_get_priority () |
---|
48 | { |
---|
49 | return g_slog_priority; |
---|
50 | } |
---|
51 | |
---|
52 | void SLOG_DECLSPEC slog_set_callback (slog_callback_t c) |
---|
53 | { |
---|
54 | g_slog_callback= c; |
---|
55 | } |
---|
56 | |
---|
57 | void SLOG_DECLSPEC slog_write (slog_priority_t prio, const char* fmt, ...) |
---|
58 | { |
---|
59 | va_list ap; |
---|
60 | va_start (ap, fmt); |
---|
61 | |
---|
62 | if (g_slog_callback) g_slog_callback(prio, fmt, ap); |
---|
63 | } |
---|
64 | |
---|
65 | static FILE* g_slog_stream_file= NULL; |
---|
66 | |
---|
67 | void SLOG_DECLSPEC slog_stream_callback (slog_priority_t prio, const char* fmt, va_list ap) |
---|
68 | { |
---|
69 | if (!g_slog_stream_file) g_slog_stream_file= stderr; |
---|
70 | |
---|
71 | if (prio<= g_slog_priority) { |
---|
72 | fprintf (g_slog_stream_file, "%s: ", slog_get_priority_name(prio)); |
---|
73 | vfprintf (g_slog_stream_file, fmt, ap); |
---|
74 | fprintf (g_slog_stream_file, "\n"); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | void SLOG_DECLSPEC slog_stream_set_stream (FILE *stream) |
---|
79 | { |
---|
80 | g_slog_stream_file= stream; |
---|
81 | } |
---|