logging.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Provides a log file to ease tracing the program.
  3. Copyright (C) 2006-2021
  4. 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_global.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_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE))
  74. return mc_config_get_string (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE,
  75. NULL);
  76. return mc_config_get_full_path ("mc.log");
  77. }
  78. /* --------------------------------------------------------------------------------------------- */
  79. static void
  80. G_GNUC_PRINTF (1, 0)
  81. mc_va_log (const char *fmt, va_list args)
  82. {
  83. char *logfilename;
  84. logfilename = get_log_filename ();
  85. if (logfilename != NULL)
  86. {
  87. FILE *f;
  88. f = fopen (logfilename, "a");
  89. if (f != NULL)
  90. {
  91. (void) vfprintf (f, fmt, args);
  92. (void) fclose (f);
  93. }
  94. g_free (logfilename);
  95. }
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. /*** public functions ****************************************************************************/
  99. /* --------------------------------------------------------------------------------------------- */
  100. void
  101. mc_log (const char *fmt, ...)
  102. {
  103. va_list args;
  104. if (!is_logging_enabled ())
  105. return;
  106. va_start (args, fmt);
  107. mc_va_log (fmt, args);
  108. va_end (args);
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. void
  112. mc_always_log (const char *fmt, ...)
  113. {
  114. va_list args;
  115. va_start (args, fmt);
  116. mc_va_log (fmt, args);
  117. va_end (args);
  118. }
  119. /* --------------------------------------------------------------------------------------------- */