Document of c-modernization-kit (util) 1.0.0
Loading...
Searching...
No Matches
syslog-provider.c
Go to the documentation of this file.
1#ifndef _WIN32
2
3#include <syslog.h>
4#include <trace-syslog-util.h>
5#include <stdlib.h>
6#include <string.h>
7
12{
14 char *ident;
15};
16
18 syslog_provider_init(const char *ident, int facility)
19{
20 syslog_provider_t *handle;
21 size_t len;
22
23 if (ident == NULL)
24 {
25 return NULL;
26 }
27
28 handle = (syslog_provider_t *)malloc(sizeof(syslog_provider_t));
29 if (handle == NULL)
30 {
31 return NULL;
32 }
33
34 /* ident 文字列を複製して保持 (openlog はポインタを保持するため) */
35 len = strlen(ident) + 1;
36 handle->ident = (char *)malloc(len);
37 if (handle->ident == NULL)
38 {
39 free(handle);
40 return NULL;
41 }
42 memcpy(handle->ident, ident, len);
43
44 openlog(handle->ident, LOG_NDELAY | LOG_PID, facility);
45
46 return handle;
47}
48
50 syslog_provider_write(syslog_provider_t *handle, int level, const char *message)
51{
52 if (handle == NULL || message == NULL)
53 {
54 return 0;
55 }
56
57 syslog(level, "%s", message);
58
59 return 0;
60}
61
64{
65 if (handle == NULL)
66 {
67 return;
68 }
69
70 closelog();
71 free(handle->ident);
72 free(handle);
73}
74
76 syslog_provider_rename(syslog_provider_t *handle, const char *new_ident)
77{
78 char *dup;
79 size_t len;
80
81 if (handle == NULL || new_ident == NULL)
82 {
83 return -1;
84 }
85
86 len = strlen(new_ident) + 1;
87 dup = (char *)malloc(len);
88 if (dup == NULL)
89 {
90 return -1;
91 }
92 memcpy(dup, new_ident, len);
93
94 closelog();
95 free(handle->ident);
96 handle->ident = dup;
97 openlog(handle->ident, LOG_NDELAY | LOG_PID, LOG_USER);
98
99 return 0;
100}
101
102#else /* _WIN32 */
103 #pragma warning(disable : 4206)
104#endif /* _WIN32 */
syslog プロバイダハンドル構造体 (内部定義)。
char * ident
openlog に渡した識別子文字列 (複製を保持)。
int TRACE_SYSLOG_UTIL_API syslog_provider_rename(syslog_provider_t *handle, const char *new_ident)
syslog プロバイダの識別子を変更する。
void TRACE_SYSLOG_UTIL_API syslog_provider_dispose(syslog_provider_t *handle)
syslog プロバイダを終了する。
syslog_provider_t *TRACE_SYSLOG_UTIL_API syslog_provider_init(const char *ident, int facility)
syslog プロバイダを初期化する。
int TRACE_SYSLOG_UTIL_API syslog_provider_write(syslog_provider_t *handle, int level, const char *message)
syslog へ UTF-8 メッセージを書き込む。
syslog ヘルパーライブラリ。
struct syslog_provider syslog_provider_t
syslog プロバイダハンドル (不透明型)。
#define TRACE_SYSLOG_UTIL_API
呼び出し規約マクロ。