Log.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/log/Log.h"
  16. #include "util/log/Log_impl.h"
  17. #include "util/CString.h"
  18. #include <stdarg.h>
  19. void Log_print(struct Log* log,
  20. enum Log_Level logLevel,
  21. const char* file,
  22. int line,
  23. const char* format,
  24. ...)
  25. {
  26. va_list args;
  27. va_start(args, format);
  28. log->print(log, logLevel, file, line, format, args);
  29. va_end(args);
  30. }
  31. void Log_print0(struct Log* log, enum Log_Level lvl, const char* file, int line, const char* msg)
  32. {
  33. Log_print(log, lvl, file, line, "%s", msg);
  34. }
  35. char* Log_nameForLevel(enum Log_Level logLevel)
  36. {
  37. switch (logLevel) {
  38. case Log_Level_KEYS: return "KEYS";
  39. case Log_Level_DEBUG: return "DEBUG";
  40. case Log_Level_INFO: return "INFO";
  41. case Log_Level_WARN: return "WARN";
  42. case Log_Level_ERROR: return "ERROR";
  43. case Log_Level_CRITICAL: return "CRITICAL";
  44. default: return "INVALID";
  45. }
  46. }
  47. enum Log_Level Log_levelForName(char* name)
  48. {
  49. for (enum Log_Level logLevel = Log_Level_KEYS; logLevel <= Log_Level_CRITICAL; logLevel++) {
  50. if (!CString_strcasecmp(name, Log_nameForLevel(logLevel))) {
  51. return logLevel;
  52. }
  53. }
  54. return Log_Level_INVALID;
  55. }