PageRenderTime 487ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/usbmodeswitch/jim/jim-syslog.c

https://gitlab.com/envieidoc/advancedtomato2
C | 190 lines | 150 code | 22 blank | 18 comment | 28 complexity | 8a4081bcf5c80acc7b48c7db841c0a1d MD5 | raw file
  1. /* Syslog interface for tcl
  2. * Copyright Victor Wagner <vitus@ice.ru> at
  3. * http://www.ice.ru/~vitus/works/tcl.html#syslog
  4. *
  5. * Slightly modified by Steve Bennett <steveb@snapgear.com>
  6. * Ported to Jim by Steve Bennett <steveb@workware.net.au>
  7. */
  8. #include <syslog.h>
  9. #include <string.h>
  10. #include "jim.h"
  11. #include "jimautoconf.h"
  12. typedef struct
  13. {
  14. int logOpened;
  15. int facility;
  16. int options;
  17. char ident[32];
  18. } SyslogInfo;
  19. #ifndef LOG_AUTHPRIV
  20. # define LOG_AUTHPRIV LOG_AUTH
  21. #endif
  22. static const char * const facilities[] = {
  23. [LOG_AUTHPRIV] = "authpriv",
  24. [LOG_CRON] = "cron",
  25. [LOG_DAEMON] = "daemon",
  26. [LOG_KERN] = "kernel",
  27. [LOG_LPR] = "lpr",
  28. [LOG_MAIL] = "mail",
  29. [LOG_NEWS] = "news",
  30. [LOG_SYSLOG] = "syslog",
  31. [LOG_USER] = "user",
  32. [LOG_UUCP] = "uucp",
  33. [LOG_LOCAL0] = "local0",
  34. [LOG_LOCAL1] = "local1",
  35. [LOG_LOCAL2] = "local2",
  36. [LOG_LOCAL3] = "local3",
  37. [LOG_LOCAL4] = "local4",
  38. [LOG_LOCAL5] = "local5",
  39. [LOG_LOCAL6] = "local6",
  40. [LOG_LOCAL7] = "local7",
  41. };
  42. static const char * const priorities[] = {
  43. [LOG_EMERG] = "emerg",
  44. [LOG_ALERT] = "alert",
  45. [LOG_CRIT] = "crit",
  46. [LOG_ERR] = "error",
  47. [LOG_WARNING] = "warning",
  48. [LOG_NOTICE] = "notice",
  49. [LOG_INFO] = "info",
  50. [LOG_DEBUG] = "debug",
  51. };
  52. /**
  53. * Deletes the syslog command.
  54. */
  55. static void Jim_SyslogCmdDelete(Jim_Interp *interp, void *privData)
  56. {
  57. SyslogInfo *info = (SyslogInfo *) privData;
  58. if (info->logOpened) {
  59. closelog();
  60. }
  61. Jim_Free(info);
  62. }
  63. /* Syslog_Log -
  64. * implements syslog tcl command. General format: syslog ?options? level text
  65. * options -facility -ident -options
  66. *
  67. * syslog ?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? text
  68. */
  69. int Jim_SyslogCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  70. {
  71. int priority = LOG_INFO;
  72. int i = 1;
  73. SyslogInfo *info = Jim_CmdPrivData(interp);
  74. if (argc <= 1) {
  75. wrongargs:
  76. Jim_WrongNumArgs(interp, 1, argv,
  77. "?-facility cron|daemon|...? ?-ident string? ?-options int? ?debug|info|...? message");
  78. return JIM_ERR;
  79. }
  80. while (i < argc - 1) {
  81. if (Jim_CompareStringImmediate(interp, argv[i], "-facility")) {
  82. int entry =
  83. Jim_FindByName(Jim_String(argv[i + 1]), facilities,
  84. sizeof(facilities) / sizeof(*facilities));
  85. if (entry < 0) {
  86. Jim_SetResultString(interp, "Unknown facility", -1);
  87. return JIM_ERR;
  88. }
  89. if (info->facility != entry) {
  90. info->facility = entry;
  91. if (info->logOpened) {
  92. closelog();
  93. info->logOpened = 0;
  94. }
  95. }
  96. }
  97. else if (Jim_CompareStringImmediate(interp, argv[i], "-options")) {
  98. long tmp;
  99. if (Jim_GetLong(interp, argv[i + 1], &tmp) == JIM_ERR) {
  100. return JIM_ERR;
  101. }
  102. info->options = tmp;
  103. if (info->logOpened) {
  104. closelog();
  105. info->logOpened = 0;
  106. }
  107. }
  108. else if (Jim_CompareStringImmediate(interp, argv[i], "-ident")) {
  109. strncpy(info->ident, Jim_String(argv[i + 1]), sizeof(info->ident));
  110. info->ident[sizeof(info->ident) - 1] = 0;
  111. if (info->logOpened) {
  112. closelog();
  113. info->logOpened = 0;
  114. }
  115. }
  116. else {
  117. break;
  118. }
  119. i += 2;
  120. }
  121. /* There should be either 0, 1 or 2 args left */
  122. if (i == argc) {
  123. /* No args, but they have set some options, so OK */
  124. return JIM_OK;
  125. }
  126. if (i < argc - 1) {
  127. priority =
  128. Jim_FindByName(Jim_String(argv[i]), priorities,
  129. sizeof(priorities) / sizeof(*priorities));
  130. if (priority < 0) {
  131. Jim_SetResultString(interp, "Unknown priority", -1);
  132. return JIM_ERR;
  133. }
  134. i++;
  135. }
  136. if (i != argc - 1) {
  137. goto wrongargs;
  138. }
  139. if (!info->logOpened) {
  140. if (!info->ident[0]) {
  141. Jim_Obj *argv0 = Jim_GetGlobalVariableStr(interp, "argv0", JIM_NONE);
  142. if (argv0) {
  143. strncpy(info->ident, Jim_String(argv0), sizeof(info->ident));
  144. }
  145. else {
  146. strcpy(info->ident, "Tcl script");
  147. }
  148. info->ident[sizeof(info->ident) - 1] = 0;
  149. }
  150. openlog(info->ident, info->options, info->facility);
  151. info->logOpened = 1;
  152. }
  153. syslog(priority, "%s", Jim_String(argv[i]));
  154. return JIM_OK;
  155. }
  156. int Jim_syslogInit(Jim_Interp *interp)
  157. {
  158. SyslogInfo *info;
  159. if (Jim_PackageProvide(interp, "syslog", "1.0", JIM_ERRMSG))
  160. return JIM_ERR;
  161. info = Jim_Alloc(sizeof(*info));
  162. info->logOpened = 0;
  163. info->options = 0;
  164. info->facility = LOG_USER;
  165. info->ident[0] = 0;
  166. Jim_CreateCommand(interp, "syslog", Jim_SyslogCmd, info, Jim_SyslogCmdDelete);
  167. return JIM_OK;
  168. }