util.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright (c) 2007 - 2010 RIPE NCC - All Rights Reserved
  3. Permission to use, copy, modify, and distribute this software and its
  4. documentation for any purpose and without fee is hereby granted, provided
  5. that the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation, and that the name of the author not be used in advertising or
  8. publicity pertaining to distribution of the software without specific,
  9. written prior permission.
  10. THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  11. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  12. AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  13. DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  14. AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. Created by Devin Bayer on 9/1/10.
  17. */
  18. #include "util.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <assert.h>
  23. #include <stdbool.h>
  24. #include <syslog.h>
  25. #include <time.h>
  26. #include <string.h>
  27. static bool use_syslog = true;
  28. void log_to_syslog() {
  29. use_syslog = true;
  30. }
  31. void log_to_stderr() {
  32. use_syslog = false;
  33. }
  34. #define log(lvl, lvl_str) \
  35. va_list args; \
  36. va_start(args, fmt); \
  37. _log(LOG_##lvl, #lvl_str, fmt, args)
  38. static char *now_str() {
  39. static char buffer[1000];
  40. time_t now = time(0);
  41. strftime(buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", localtime(&now));
  42. return buffer;
  43. }
  44. static void _log(int lvl, char *lvl_str, const char *fmt, va_list args) {
  45. if(use_syslog) {
  46. syslog(lvl, fmt, args);
  47. } else {
  48. char prefix[strlen(fmt) + 1000];
  49. sprintf(prefix, "%s [%s] %s\n", now_str(), lvl_str, fmt);
  50. vfprintf(stderr, prefix, args);
  51. }
  52. }
  53. void err(const char *fmt, ...) { log(ERR, error); }
  54. void warn(const char *fmt, ...) { log(WARNING, warn); }
  55. void debug(const char *fmt, ...) { log(INFO, info); }
  56. int time2str(struct tm* date,char *time_str)
  57. {
  58. return sprintf(time_str, "%02d/%02d/%02d %02d:%02d:%02d", date->tm_mon+1, date->tm_mday, date->tm_year%100,
  59. date->tm_hour, date->tm_min, date->tm_sec);
  60. }
  61. int int2str(uint32_t value, char* str)
  62. {
  63. return sprintf(str, "%u", value);
  64. }
  65. static void ti2s(uint32_t value) {
  66. char buf[100], ref[100];
  67. sprintf(ref, "%u", value);
  68. int len = int2str(value, buf);
  69. printf("%s =?= %s (%i)\n", ref, buf, len);
  70. }
  71. void test_utils()
  72. {
  73. ti2s(0);
  74. ti2s(99999);
  75. ti2s(4294967295L);
  76. }