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,
27 * \brief Source: provides a log file to ease tracing the program
35 #include "lib/global.h"
36 #include "lib/mcconfig.h"
37 #include "lib/fileloc.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 /* --------------------------------------------------------------------------------------------- */
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
)
68 logging_enabled
= (*env_is_enabled
== '1' || g_ascii_strcasecmp (env_is_enabled
, "true") == 0);
69 logging_initialized
= TRUE
;
73 /* --------------------------------------------------------------------------------------------- */
76 is_logging_enabled (void)
79 if (logging_initialized
)
80 return logging_enabled
;
82 if (is_logging_enabled_from_env ())
83 return 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 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
112 mc_va_log (const char *fmt
, va_list args
)
117 logfilename
= get_log_filename ();
119 if (logfilename
!= NULL
)
121 f
= fopen (logfilename
, "a");
124 (void) vfprintf (f
, fmt
, args
);
127 g_free (logfilename
);
132 /* --------------------------------------------------------------------------------------------- */
133 /*** public functions ****************************************************************************/
134 /* --------------------------------------------------------------------------------------------- */
137 mc_log (const char *fmt
, ...)
141 if (!is_logging_enabled ())
144 va_start (args
, fmt
);
145 mc_va_log (fmt
, args
);
149 /* --------------------------------------------------------------------------------------------- */
152 mc_always_log (const char *fmt
, ...)
156 va_start (args
, fmt
);
157 mc_va_log (fmt
, args
);
161 /* --------------------------------------------------------------------------------------------- */