logging.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 =
  63. mc_config_get_bool (mc_global.main_config, CONFIG_GROUP_NAME, CONFIG_KEY_NAME, 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
  82. G_GNUC_PRINTF (1, 0)
  83. mc_va_log (const char *fmt, va_list args)
  84. {
  85. char *logfilename;
  86. logfilename = get_log_filename ();
  87. if (logfilename != NULL)
  88. {
  89. FILE *f;
  90. f = fopen (logfilename, "a");
  91. if (f != NULL)
  92. {
  93. (void) vfprintf (f, fmt, args);
  94. (void) fclose (f);
  95. }
  96. g_free (logfilename);
  97. }
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. /*** public functions ****************************************************************************/
  101. /* --------------------------------------------------------------------------------------------- */
  102. void
  103. mc_log (const char *fmt, ...)
  104. {
  105. va_list args;
  106. if (!is_logging_enabled ())
  107. return;
  108. va_start (args, fmt);
  109. mc_va_log (fmt, args);
  110. va_end (args);
  111. }
  112. /* --------------------------------------------------------------------------------------------- */
  113. void
  114. mc_always_log (const char *fmt, ...)
  115. {
  116. va_list args;
  117. va_start (args, fmt);
  118. mc_va_log (fmt, args);
  119. va_end (args);
  120. }
  121. /* --------------------------------------------------------------------------------------------- */