1 /*
2 Provides a log file to ease tracing the program.
3
4 Copyright (C) 2006-2025
5 Free Software Foundation, Inc.
6
7 Written by:
8 Roland Illig <roland.illig@gmx.de>, 2006
9 Slava Zanko <slavazanko@gmail.com>, 2009, 2011
10
11 This file is part of the Midnight Commander.
12
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
17
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
27 /** \file logging.c
28 * \brief Source: provides a log file to ease tracing the program
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 /*** global variables ****************************************************************************/
43
44 /*** file scope macro definitions ****************************************************************/
45
46 #define CONFIG_GROUP_NAME "Development"
47 #define CONFIG_KEY_NAME "logging"
48 #define CONFIG_KEY_NAME_FILE "logfile"
49
50 /*** file scope type declarations ****************************************************************/
51
52 /*** forward declarations (file scope functions) *************************************************/
53
54 /*** file scope variables ************************************************************************/
55
56 static gboolean logging_initialized = FALSE;
57 static gboolean logging_enabled = FALSE;
58
59 /* --------------------------------------------------------------------------------------------- */
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
62
63 static gboolean
64 is_logging_enabled_from_env (void)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 G_GNUC_PRINTF (1, 0)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
117 mc_va_log (const char *fmt, va_list args)
118 {
119 char *logfilename;
120
121 logfilename = get_log_filename ();
122
123 if (logfilename != NULL)
124 {
125 FILE *f;
126
127 f = fopen (logfilename, "a");
128 if (f != NULL)
129 {
130 (void) vfprintf (f, fmt, args);
131 (void) fclose (f);
132 }
133 g_free (logfilename);
134 }
135 }
136
137 /* --------------------------------------------------------------------------------------------- */
138 /*** public functions ****************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
140
141 void
142 mc_log (const char *fmt, ...)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
143 {
144 va_list args;
145
146 if (!is_logging_enabled ())
147 return;
148
149 va_start (args, fmt);
150 mc_va_log (fmt, args);
151 va_end (args);
152 }
153
154 /* --------------------------------------------------------------------------------------------- */
155
156 void
157 mc_always_log (const char *fmt, ...)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
158 {
159 va_list args;
160
161 va_start (args, fmt);
162 mc_va_log (fmt, args);
163 va_end (args);
164 }
165
166 /* --------------------------------------------------------------------------------------------- */