You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

log.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* This file is part of Netsukuku system
  2. * (c) Copyright 2004 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifndef LOG_H
  19. #define LOG_H
  20. #include <stdarg.h>
  21. /*
  22. * Use ERROR_MSG and ERROR_POS in this way:
  23. * printf(ERROR_MSG "damn! damn! damn!", ERROR_POS);
  24. * printf(ERROR_MSG "damn! damn! damn!", ERROR_FUNC);
  25. */
  26. #define ERROR_MSG "%s:%d: "
  27. #define ERROR_POS __FILE__, __LINE__
  28. #define ERROR_FUNC __FUNCTION__, __LINE__
  29. /*Debug levels*/
  30. #define DBG_NORMAL 1
  31. #define DBG_SOFT 2
  32. #define DBG_NOISE 3
  33. #define DBG_INSANE 4
  34. /*
  35. * ERROR_FINISH:
  36. * A kind way to say all was messed up, take this example:
  37. *
  38. * int func(void) // returns -1 on errors
  39. * {
  40. * int ret=0;
  41. *
  42. * ,,,BLA BLA...
  43. *
  44. * if(error_condition)
  45. * ERROR_FINISH(ret, -1, finish);
  46. *
  47. * error_condition &&
  48. * ERROR_FINISH(ret, -1, finish);
  49. *
  50. * ,,,BLA BLA...
  51. *
  52. * finish:
  53. * return ret;
  54. * }
  55. */
  56. #define ERROR_FINISH(ret, err, label_finish) \
  57. ({ \
  58. void *_label_finish=&&label_finish; \
  59. (ret)=(err); \
  60. goto *_label_finish; \
  61. \
  62. (ret); /* in this way gcc thinks this macro returns
  63. an integer */ \
  64. })
  65. #ifdef DEBUG
  66. /* Colors used to highlights things while debugging ;) */
  67. #define DEFCOL "\033[0m"
  68. #define BLACK(x) "\033[0;30m" x DEFCOL
  69. #define RED(x) "\033[0;31m" x DEFCOL
  70. #define GREEN(x) "\033[0;32m" x DEFCOL
  71. #define BROWN(x) "\033[0;33m" x DEFCOL
  72. #define BLUE(x) "\033[0;34m" x DEFCOL
  73. #define PURPLE(x) "\033[0;35m" x DEFCOL
  74. #define CYAN(x) "\033[0;36m" x DEFCOL
  75. #define LIGHTGRAY(x) "\033[0;37m" x DEFCOL
  76. #define DARKGRAY(x) "\033[1;30m" x DEFCOL
  77. #define LIGHTRED(x) "\033[1;31m" x DEFCOL
  78. #define LIGHTGREEN(x) "\033[1;32m" x DEFCOL
  79. #define YELLOW(x) "\033[1;33m" x DEFCOL
  80. #define LIGHTBLUE(x) "\033[1;34m" x DEFCOL
  81. #define MAGENTA(x) "\033[1;35m" x DEFCOL
  82. #define LIGHTCYAN(x) "\033[1;36m" x DEFCOL
  83. #define WHITE(x) "\033[1;37m" x DEFCOL
  84. #else
  85. #define BLACK(x) x
  86. #define RED(x) x
  87. #define GREEN(x) x
  88. #define BROWN(x) x
  89. #define BLUE(x) x
  90. #define PURPLE(x) x
  91. #define CYAN(x) x
  92. #define LIGHTGRAY(x) x
  93. #define DARKGRAY(x) x
  94. #define LIGHTRED(x) x
  95. #define LIGHTGREEN(x) x
  96. #define YELLOW(x) x
  97. #define LIGHTBLUE(x) x
  98. #define MAGENTA(x) x
  99. #define LIGHTCYAN(x) x
  100. #define WHITE(x) x
  101. #endif
  102. /* functions declaration */
  103. void log_init(char *, int, int);
  104. int log_to_file(char *filename);
  105. void close_log_file(void);
  106. void fatal(const char *, ...) __attribute__ ((noreturn));
  107. void error(const char *, ...);
  108. void loginfo(const char *, ...);
  109. void debug(int lvl, const char *, ...);
  110. void print_log(int level, const char *fmt, va_list args);
  111. #endif /*LOG_H */