log.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * Redistribution of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * Neither the name of Sun Microsystems, Inc. or the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * This software is provided "AS IS," without a warranty of any kind.
  20. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  21. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  23. * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  24. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  25. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  26. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  27. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  28. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  29. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  30. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  31. */
  32. #define _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 || \
  33. _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || \
  34. /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
  35. #include <unistd.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <syslog.h>
  39. #include <errno.h>
  40. #include <stdarg.h>
  41. #include <string.h>
  42. #include <ipmitool/log.h>
  43. struct logpriv_s {
  44. char * name;
  45. int daemon;
  46. int level;
  47. };
  48. struct logpriv_s *logpriv;
  49. static void log_reinit(void)
  50. {
  51. log_init(NULL, 0, 0);
  52. }
  53. void lprintf(int level, const char * format, ...)
  54. {
  55. static char logmsg[LOG_MSG_LENGTH];
  56. va_list vptr;
  57. if (!logpriv)
  58. log_reinit();
  59. if (logpriv->level < level)
  60. return;
  61. va_start(vptr, format);
  62. vsnprintf(logmsg, LOG_MSG_LENGTH, format, vptr);
  63. va_end(vptr);
  64. if (logpriv->daemon)
  65. syslog(level, "%s", logmsg);
  66. else
  67. fprintf(stderr, "%s\n", logmsg);
  68. return;
  69. }
  70. void lperror(int level, const char * format, ...)
  71. {
  72. static char logmsg[LOG_MSG_LENGTH];
  73. va_list vptr;
  74. if (!logpriv)
  75. log_reinit();
  76. if (logpriv->level < level)
  77. return;
  78. va_start(vptr, format);
  79. vsnprintf(logmsg, LOG_MSG_LENGTH, format, vptr);
  80. va_end(vptr);
  81. if (logpriv->daemon)
  82. syslog(level, "%s: %s", logmsg, strerror(errno));
  83. else
  84. fprintf(stderr, "%s: %s\n", logmsg, strerror(errno));
  85. return;
  86. }
  87. /*
  88. * open connection to syslog if daemon
  89. */
  90. void log_init(const char * name, int isdaemon, int verbose)
  91. {
  92. if (logpriv)
  93. return;
  94. logpriv = malloc(sizeof(struct logpriv_s));
  95. if (!logpriv)
  96. return;
  97. if (name != NULL)
  98. logpriv->name = strdup(name);
  99. else
  100. logpriv->name = strdup(LOG_NAME_DEFAULT);
  101. if (logpriv->name == NULL)
  102. fprintf(stderr, "ipmitool: malloc failure\n");
  103. logpriv->daemon = isdaemon;
  104. logpriv->level = verbose + LOG_NOTICE;
  105. if (logpriv->daemon)
  106. openlog(logpriv->name, LOG_CONS, LOG_LOCAL4);
  107. }
  108. /*
  109. * stop syslog logging if daemon mode,
  110. * free used memory that stored log service
  111. */
  112. void log_halt(void)
  113. {
  114. if (!logpriv)
  115. return;
  116. if (logpriv->name) {
  117. free(logpriv->name);
  118. logpriv->name = NULL;
  119. }
  120. if (logpriv->daemon)
  121. closelog();
  122. free(logpriv);
  123. logpriv = NULL;
  124. }
  125. int log_level_get(void)
  126. {
  127. return logpriv->level;
  128. }
  129. void log_level_set(int level)
  130. {
  131. logpriv->level = level;
  132. }