log.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * File created by David Delassus.
  4. * For license, see COPYING.
  5. */
  6. #include "wmfs.h"
  7. #include "util.h"
  8. void
  9. log_init(void)
  10. {
  11. char *path = NULL;
  12. xasprintf(&path, "%s/wmfs-%s.log", P_tmpdir, DisplayString(W->dpy));
  13. if(path)
  14. {
  15. /* open log */
  16. if(!(W->log = fopen(path, "a")))
  17. warnx("Can't open log file '%s': %s\n", path, strerror(errno));
  18. free(path);
  19. }
  20. }
  21. /** Print a warning on standard error output and in the log file
  22. * \param format Format string (same syntax as printf)
  23. */
  24. void
  25. warnl(const char *format, ...)
  26. {
  27. va_list args;
  28. if(W->log)
  29. {
  30. va_start(args, format);
  31. vfprintf(W->log, format, args);
  32. fprintf(W->log, "\n");
  33. va_end(args);
  34. }
  35. va_start(args, format);
  36. vwarn(format, args);
  37. va_end(args);
  38. }
  39. /** Print an error on standard error output and in the log file
  40. * \param eval Exit value
  41. * \param format Format string (same syntax as printf)
  42. */
  43. void
  44. errl(int eval, const char *format, ...)
  45. {
  46. va_list args;
  47. if(W->log)
  48. {
  49. va_start(args, format);
  50. vfprintf(W->log, format, args);
  51. fprintf(W->log, "\n");
  52. va_end(args);
  53. fclose (W->log);
  54. }
  55. va_start(args, format);
  56. verr(eval, format, args);
  57. }
  58. /** Print a warning on standard error output and in the log file
  59. * \param format Format string (same syntax as printf)
  60. */
  61. void
  62. warnxl(const char *format, ...)
  63. {
  64. va_list args;
  65. if(W->log)
  66. {
  67. va_start(args, format);
  68. vfprintf(W->log, format, args);
  69. fprintf(W->log, "\n");
  70. va_end(args);
  71. }
  72. va_start(args, format);
  73. vwarnx(format, args);
  74. va_end(args);
  75. }
  76. /** Print an error on standard error output and in the log file
  77. * \param eval Exit value
  78. * \param format Format string (same syntax as printf)
  79. */
  80. void
  81. errxl(int eval, const char *format, ...)
  82. {
  83. va_list args;
  84. if(W->log)
  85. {
  86. va_start(args, format);
  87. vfprintf(W->log, format, args);
  88. fprintf(W->log, "\n");
  89. va_end(args);
  90. fclose (W->log);
  91. }
  92. va_start(args, format);
  93. verrx(eval, format, args);
  94. }