Merge branch '3821_init_st_mtim'
[midnight-commander.git] / lib / logging.c
blob0e106280a3dbd22520450a31be9d6a5675d9846b
1 /*
2 Provides a log file to ease tracing the program.
4 Copyright (C) 2006-2017
5 Free Software Foundation, Inc.
7 Written by:
8 Roland Illig <roland.illig@gmx.de>, 2006
9 Slava Zanko <slavazanko@gmail.com>, 2009, 2011
11 This file is part of the Midnight Commander.
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.
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.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file logging.c
28 * \brief Source: provides a log file to ease tracing the program
31 #include <config.h>
33 #include <stdarg.h>
34 #include <stdio.h>
36 #include "lib/global.h"
37 #include "lib/mcconfig.h"
38 #include "lib/fileloc.h"
40 #include "logging.h"
42 /*** global variables ****************************************************************************/
44 /*** file scope macro definitions ****************************************************************/
46 #define CONFIG_GROUP_NAME "Development"
47 #define CONFIG_KEY_NAME "logging"
48 #define CONFIG_KEY_NAME_FILE "logfile"
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 static gboolean logging_initialized = FALSE;
55 static gboolean logging_enabled = FALSE;
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 static gboolean
61 is_logging_enabled_from_env (void)
63 const char *env_is_enabled;
65 env_is_enabled = g_getenv ("MC_LOG_ENABLE");
66 if (env_is_enabled == NULL)
67 return FALSE;
69 logging_enabled = (*env_is_enabled == '1' || g_ascii_strcasecmp (env_is_enabled, "true") == 0);
70 logging_initialized = TRUE;
71 return TRUE;
74 /* --------------------------------------------------------------------------------------------- */
76 static gboolean
77 is_logging_enabled (void)
80 if (logging_initialized)
81 return logging_enabled;
83 if (is_logging_enabled_from_env ())
84 return logging_enabled;
86 logging_enabled =
87 mc_config_get_bool (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME, FALSE);
88 logging_initialized = TRUE;
90 return logging_enabled;
93 /* --------------------------------------------------------------------------------------------- */
95 static char *
96 get_log_filename (void)
98 const char *env_filename;
100 env_filename = g_getenv ("MC_LOG_FILE");
101 if (env_filename != NULL)
102 return g_strdup (env_filename);
104 if (mc_config_has_param (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE))
105 return mc_config_get_string (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE,
106 NULL);
108 return mc_config_get_full_path ("mc.log");
111 /* --------------------------------------------------------------------------------------------- */
113 static void
114 G_GNUC_PRINTF (1, 0)
115 mc_va_log (const char *fmt, va_list args)
117 char *logfilename;
119 logfilename = get_log_filename ();
121 if (logfilename != NULL)
123 FILE *f;
125 f = fopen (logfilename, "a");
126 if (f != NULL)
128 (void) vfprintf (f, fmt, args);
129 (void) fclose (f);
131 g_free (logfilename);
136 /* --------------------------------------------------------------------------------------------- */
137 /*** public functions ****************************************************************************/
138 /* --------------------------------------------------------------------------------------------- */
140 void
141 mc_log (const char *fmt, ...)
143 va_list args;
145 if (!is_logging_enabled ())
146 return;
148 va_start (args, fmt);
149 mc_va_log (fmt, args);
150 va_end (args);
153 /* --------------------------------------------------------------------------------------------- */
155 void
156 mc_always_log (const char *fmt, ...)
158 va_list args;
160 va_start (args, fmt);
161 mc_va_log (fmt, args);
162 va_end (args);
165 /* --------------------------------------------------------------------------------------------- */