logging.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "logging.h"
  27. #include "lib/mcconfig.h"
  28. #include "lib/fileloc.h"
  29. #include "src/setup.h"
  30. /*** file scope functions **********************************************/
  31. static gboolean
  32. is_logging_enabled(void)
  33. {
  34. static gboolean logging_initialized = FALSE;
  35. static gboolean logging_enabled = FALSE;
  36. if (!logging_initialized) {
  37. logging_enabled = mc_config_get_int (mc_main_config,
  38. CONFIG_APP_SECTION, "development.enable_logging", FALSE);
  39. logging_initialized = TRUE;
  40. }
  41. return logging_enabled;
  42. }
  43. /*** public functions **************************************************/
  44. void
  45. mc_log(const char *fmt, ...)
  46. {
  47. va_list args;
  48. FILE *f;
  49. char *logfilename;
  50. if (is_logging_enabled()) {
  51. va_start(args, fmt);
  52. logfilename = g_strdup_printf("%s/%s/log", home_dir, MC_USERCONF_DIR);
  53. if (logfilename != NULL) {
  54. f = fopen (logfilename, "a");
  55. if (f != NULL) {
  56. (void)vfprintf(f, fmt, args);
  57. (void)fclose(f);
  58. }
  59. g_free(logfilename);
  60. va_end(args);
  61. }
  62. }
  63. }