logging.c 4.5 KB

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