logging.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "src/main.h" /* home_dir */
  29. #include "logging.h"
  30. /*** global variables ****************************************************************************/
  31. /*** file scope macro definitions ****************************************************************/
  32. /*** file scope type declarations ****************************************************************/
  33. /*** file scope variables ************************************************************************/
  34. /*** file scope functions ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. static gboolean
  37. is_logging_enabled (void)
  38. {
  39. static gboolean logging_initialized = FALSE;
  40. static gboolean logging_enabled = FALSE;
  41. if (!logging_initialized)
  42. {
  43. logging_enabled = mc_config_get_bool (mc_main_config,
  44. CONFIG_APP_SECTION, "development.enable_logging",
  45. FALSE);
  46. logging_initialized = TRUE;
  47. }
  48. return logging_enabled;
  49. }
  50. /* --------------------------------------------------------------------------------------------- */
  51. /*** public functions ****************************************************************************/
  52. /* --------------------------------------------------------------------------------------------- */
  53. void
  54. mc_log (const char *fmt, ...)
  55. {
  56. va_list args;
  57. FILE *f;
  58. char *logfilename;
  59. if (is_logging_enabled ())
  60. {
  61. va_start (args, fmt);
  62. logfilename = g_strdup_printf ("%s/%s/log", home_dir, MC_USERCONF_DIR);
  63. if (logfilename != NULL)
  64. {
  65. f = fopen (logfilename, "a");
  66. if (f != NULL)
  67. {
  68. (void) vfprintf (f, fmt, args);
  69. (void) fclose (f);
  70. }
  71. g_free (logfilename);
  72. va_end (args);
  73. }
  74. }
  75. }
  76. /* --------------------------------------------------------------------------------------------- */