logging.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Provides a log file to ease tracing the program.
  3. Copyright (C) 2006, 2009 Free Software Foundation, Inc.
  4. Written: 2006 Roland Illig <roland.illig@gmx.de>.
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software; you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The Midnight Commander is distributed in the hope that it will be
  11. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  12. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. MA 02110-1301, USA.
  18. */
  19. /** \file logging.c
  20. * \brief Source: provides a log file to ease tracing the program
  21. */
  22. #include <config.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include "lib/global.h"
  26. #include "lib/mcconfig.h"
  27. #include "lib/fileloc.h"
  28. #include "logging.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. #define CONFIG_GROUP_NAME "Development"
  32. #define CONFIG_KEY_NAME "logging"
  33. #define CONFIG_KEY_NAME_FILE "logfile"
  34. /*** file scope type declarations ****************************************************************/
  35. /*** file scope variables ************************************************************************/
  36. static gboolean logging_initialized = FALSE;
  37. static gboolean logging_enabled = FALSE;
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. static gboolean
  41. is_logging_enabled_from_env (void)
  42. {
  43. const char *env_is_enabled;
  44. env_is_enabled = g_getenv ("MC_LOG_ENABLE");
  45. if (env_is_enabled == NULL)
  46. return FALSE;
  47. logging_enabled = (*env_is_enabled == '1' || g_ascii_strcasecmp (env_is_enabled, "true") == 0);
  48. logging_initialized = TRUE;
  49. return TRUE;
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. static gboolean
  53. is_logging_enabled (void)
  54. {
  55. if (logging_initialized)
  56. return logging_enabled;
  57. if (is_logging_enabled_from_env ())
  58. return logging_enabled;
  59. logging_enabled =
  60. mc_config_get_bool (mc_main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME, FALSE);
  61. logging_initialized = TRUE;
  62. return logging_enabled;
  63. }
  64. /* --------------------------------------------------------------------------------------------- */
  65. static char *
  66. get_log_filename (void)
  67. {
  68. const char *env_filename;
  69. env_filename = g_getenv ("MC_LOG_FILE");
  70. if (env_filename != NULL)
  71. return g_strdup (env_filename);
  72. if (mc_config_has_param (mc_main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE))
  73. return mc_config_get_string (mc_main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE, NULL);
  74. return g_build_filename (mc_config_get_cache_path (), "mc.log", NULL);
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. static void
  78. mc_va_log (const char *fmt, va_list args)
  79. {
  80. FILE *f;
  81. char *logfilename;
  82. logfilename = get_log_filename ();
  83. if (logfilename != NULL)
  84. {
  85. f = fopen (logfilename, "a");
  86. if (f != NULL)
  87. {
  88. (void) vfprintf (f, fmt, args);
  89. (void) fclose (f);
  90. }
  91. g_free (logfilename);
  92. }
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. /*** public functions ****************************************************************************/
  96. /* --------------------------------------------------------------------------------------------- */
  97. void
  98. mc_log (const char *fmt, ...)
  99. {
  100. va_list args;
  101. if (!is_logging_enabled ())
  102. return;
  103. va_start (args, fmt);
  104. mc_va_log (fmt, args);
  105. va_end (args);
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. void
  109. mc_always_log (const char *fmt, ...)
  110. {
  111. va_list args;
  112. va_start (args, fmt);
  113. mc_va_log (fmt, args);
  114. va_end (args);
  115. }
  116. /* --------------------------------------------------------------------------------------------- */