loglevel.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @file loglevel.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Log level specification parsing function.
  25. */
  26. #ifndef BADVPN_MISC_LOGLEVEL_H
  27. #define BADVPN_MISC_LOGLEVEL_H
  28. #include <string.h>
  29. #include <base/BLog.h>
  30. /**
  31. * Parses the log level string.
  32. *
  33. * @param str log level string. Recognizes none, error, warning, notice,
  34. * info, debug.
  35. * @return 0 for none, one of BLOG_* for some log level, -1 for unrecognized
  36. */
  37. static int parse_loglevel (char *str);
  38. int parse_loglevel (char *str)
  39. {
  40. if (!strcmp(str, "none")) {
  41. return 0;
  42. }
  43. if (!strcmp(str, "error")) {
  44. return BLOG_ERROR;
  45. }
  46. if (!strcmp(str, "warning")) {
  47. return BLOG_WARNING;
  48. }
  49. if (!strcmp(str, "notice")) {
  50. return BLOG_NOTICE;
  51. }
  52. if (!strcmp(str, "info")) {
  53. return BLOG_INFO;
  54. }
  55. if (!strcmp(str, "debug")) {
  56. return BLOG_DEBUG;
  57. }
  58. char *endptr;
  59. int res = strtol(str, &endptr, 10);
  60. if (*str && !*endptr && res >= 0 && res <= BLOG_DEBUG) {
  61. return res;
  62. }
  63. return -1;
  64. }
  65. #endif