Ticket #2569: fix bashisms in extfs
[midnight-commander.git] / lib / logging.c
blob430598c4cc661c4b14b313967fd1cedb94910a4d
1 /*
2 Provides a log file to ease tracing the program.
4 Copyright (C) 2006, 2009 Free Software Foundation, Inc.
6 Written: 2006 Roland Illig <roland.illig@gmx.de>.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be
16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 MA 02110-1301, USA.
26 /** \file logging.c
27 * \brief Source: provides a log file to ease tracing the program
30 #include <config.h>
32 #include <stdarg.h>
33 #include <stdio.h>
35 #include "lib/global.h"
36 #include "lib/mcconfig.h"
37 #include "lib/fileloc.h"
39 #include "logging.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 #define CONFIG_GROUP_NAME "Development"
46 #define CONFIG_KEY_NAME "logging"
47 #define CONFIG_KEY_NAME_FILE "logfile"
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 static gboolean logging_initialized = FALSE;
54 static gboolean logging_enabled = FALSE;
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
59 static gboolean
60 is_logging_enabled_from_env (void)
62 const char *env_is_enabled;
64 env_is_enabled = g_getenv ("MC_LOG_ENABLE");
65 if (env_is_enabled == NULL)
66 return FALSE;
68 logging_enabled = (*env_is_enabled == '1' || g_ascii_strcasecmp (env_is_enabled, "true") == 0);
69 logging_initialized = TRUE;
70 return TRUE;
73 /* --------------------------------------------------------------------------------------------- */
75 static gboolean
76 is_logging_enabled (void)
79 if (logging_initialized)
80 return logging_enabled;
82 if (is_logging_enabled_from_env ())
83 return logging_enabled;
85 logging_enabled =
86 mc_config_get_bool (mc_main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME, FALSE);
87 logging_initialized = TRUE;
89 return logging_enabled;
92 /* --------------------------------------------------------------------------------------------- */
94 static char *
95 get_log_filename (void)
97 const char *env_filename;
99 env_filename = g_getenv ("MC_LOG_FILE");
100 if (env_filename != NULL)
101 return g_strdup (env_filename);
103 if (mc_config_has_param (mc_main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE))
104 return mc_config_get_string (mc_main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE, NULL);
106 return g_build_filename (mc_config_get_cache_path (), "mc.log", NULL);
109 /* --------------------------------------------------------------------------------------------- */
111 static void
112 mc_va_log (const char *fmt, va_list args)
114 FILE *f;
115 char *logfilename;
117 logfilename = get_log_filename ();
119 if (logfilename != NULL)
121 f = fopen (logfilename, "a");
122 if (f != NULL)
124 (void) vfprintf (f, fmt, args);
125 (void) fclose (f);
127 g_free (logfilename);
132 /* --------------------------------------------------------------------------------------------- */
133 /*** public functions ****************************************************************************/
134 /* --------------------------------------------------------------------------------------------- */
136 void
137 mc_log (const char *fmt, ...)
139 va_list args;
141 if (!is_logging_enabled ())
142 return;
144 va_start (args, fmt);
145 mc_va_log (fmt, args);
146 va_end (args);
149 /* --------------------------------------------------------------------------------------------- */
151 void
152 mc_always_log (const char *fmt, ...)
154 va_list args;
156 va_start (args, fmt);
157 mc_va_log (fmt, args);
158 va_end (args);
161 /* --------------------------------------------------------------------------------------------- */