logging.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Provides a log file to ease tracing the program.
  3. Copyright (C) 2006-2024
  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. /*** forward declarations (file scope functions) *************************************************/
  37. /*** file scope variables ************************************************************************/
  38. static gboolean logging_initialized = FALSE;
  39. static gboolean logging_enabled = FALSE;
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. static gboolean
  44. is_logging_enabled_from_env (void)
  45. {
  46. const char *env_is_enabled;
  47. env_is_enabled = g_getenv ("MC_LOG_ENABLE");
  48. if (env_is_enabled == NULL)
  49. return FALSE;
  50. logging_enabled = (*env_is_enabled == '1' || g_ascii_strcasecmp (env_is_enabled, "true") == 0);
  51. logging_initialized = TRUE;
  52. return TRUE;
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. static gboolean
  56. is_logging_enabled (void)
  57. {
  58. if (logging_initialized)
  59. return logging_enabled;
  60. if (is_logging_enabled_from_env ())
  61. return logging_enabled;
  62. logging_enabled = mc_config_get_bool (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME,
  63. FALSE);
  64. logging_initialized = TRUE;
  65. return logging_enabled;
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. static char *
  69. get_log_filename (void)
  70. {
  71. const char *env_filename;
  72. env_filename = g_getenv ("MC_LOG_FILE");
  73. if (env_filename != NULL)
  74. return g_strdup (env_filename);
  75. if (mc_config_has_param (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE))
  76. return mc_config_get_string (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME_FILE,
  77. NULL);
  78. return mc_config_get_full_path ("mc.log");
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. static void G_GNUC_PRINTF (1, 0)
  82. mc_va_log (const char *fmt, va_list args)
  83. {
  84. char *logfilename;
  85. logfilename = get_log_filename ();
  86. if (logfilename != NULL)
  87. {
  88. FILE *f;
  89. f = fopen (logfilename, "a");
  90. if (f != NULL)
  91. {
  92. (void) vfprintf (f, fmt, args);
  93. (void) fclose (f);
  94. }
  95. g_free (logfilename);
  96. }
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. /*** public functions ****************************************************************************/
  100. /* --------------------------------------------------------------------------------------------- */
  101. void
  102. mc_log (const char *fmt, ...)
  103. {
  104. va_list args;
  105. if (!is_logging_enabled ())
  106. return;
  107. va_start (args, fmt);
  108. mc_va_log (fmt, args);
  109. va_end (args);
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. mc_always_log (const char *fmt, ...)
  114. {
  115. va_list args;
  116. va_start (args, fmt);
  117. mc_va_log (fmt, args);
  118. va_end (args);
  119. }
  120. /* --------------------------------------------------------------------------------------------- */