This source file includes following definitions.
- is_logging_enabled_from_env
- is_logging_enabled
- get_log_filename
- G_GNUC_PRINTF
- mc_log
- mc_always_log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 #include <config.h>
32
33 #include <stdarg.h>
34 #include <stdio.h>
35
36 #include "lib/global.h"
37 #include "lib/mcconfig.h"
38 #include "lib/fileloc.h"
39
40 #include "logging.h"
41
42
43
44
45
46 #define CONFIG_GROUP_NAME "Development"
47 #define CONFIG_KEY_NAME "logging"
48 #define CONFIG_KEY_NAME_FILE "logfile"
49
50
51
52
53
54
55
56 static gboolean logging_initialized = FALSE;
57 static gboolean logging_enabled = FALSE;
58
59
60
61
62
63 static gboolean
64 is_logging_enabled_from_env (void)
65 {
66 const char *env_is_enabled;
67
68 env_is_enabled = g_getenv ("MC_LOG_ENABLE");
69 if (env_is_enabled == NULL)
70 return FALSE;
71
72 logging_enabled = (*env_is_enabled == '1' || g_ascii_strcasecmp (env_is_enabled, "true") == 0);
73 logging_initialized = TRUE;
74 return TRUE;
75 }
76
77
78
79 static gboolean
80 is_logging_enabled (void)
81 {
82
83 if (logging_initialized)
84 return logging_enabled;
85
86 if (is_logging_enabled_from_env ())
87 return logging_enabled;
88
89 logging_enabled =
90 mc_config_get_bool (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME, FALSE);
91 logging_initialized = TRUE;
92
93 return logging_enabled;
94 }
95
96
97
98 static char *
99 get_log_filename (void)
100 {
101 const char *env_filename;
102
103 env_filename = g_getenv ("MC_LOG_FILE");
104 if (env_filename != NULL)
105 return g_strdup (env_filename);
106
107 if (mc_config_has_param (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE))
108 return mc_config_get_string (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE,
109 NULL);
110
111 return mc_config_get_full_path ("mc.log");
112 }
113
114
115
116 static void
117 G_GNUC_PRINTF (1, 0)
118 mc_va_log (const char *fmt, va_list args)
119 {
120 char *logfilename;
121
122 logfilename = get_log_filename ();
123
124 if (logfilename != NULL)
125 {
126 FILE *f;
127
128 f = fopen (logfilename, "a");
129 if (f != NULL)
130 {
131 (void) vfprintf (f, fmt, args);
132 (void) fclose (f);
133 }
134 g_free (logfilename);
135 }
136
137 }
138
139
140
141
142
143 void
144 mc_log (const char *fmt, ...)
145 {
146 va_list args;
147
148 if (!is_logging_enabled ())
149 return;
150
151 va_start (args, fmt);
152 mc_va_log (fmt, args);
153 va_end (args);
154 }
155
156
157
158 void
159 mc_always_log (const char *fmt, ...)
160 {
161 va_list args;
162
163 va_start (args, fmt);
164 mc_va_log (fmt, args);
165 va_end (args);
166 }
167
168