2 * Copyright 2006-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include <SupportDefs.h>
18 #define DEFAULT_PRIORITY LOG_DEBUG
19 #define DEFAULT_FACILITY LOG_USER
22 extern const char *__progname
;
23 static const char *sProgramName
= __progname
;
31 static mapping sPriorities
[] = {
32 { "emerg", LOG_EMERG
},
33 { "panic", LOG_PANIC
},
34 { "alert", LOG_ALERT
},
35 { "critical", LOG_CRIT
},
39 { "warning", LOG_WARNING
},
40 { "warn", LOG_WARNING
},
41 { "notice", LOG_NOTICE
},
43 { "debug", LOG_DEBUG
},
44 { NULL
, DEFAULT_PRIORITY
},
47 static mapping sFacilities
[] = {
48 { "kernel", LOG_KERN
},
52 { "daemon", LOG_DAEMON
},
54 { "security", LOG_AUTH
},
55 { "syslog", LOG_SYSLOG
},
60 { "authpriv", LOG_AUTHPRIV
},
61 { NULL
, DEFAULT_FACILITY
},
66 lookup_mapping(struct mapping
* mappings
, const char *string
)
69 for (i
= 0; mappings
[i
].name
!= NULL
; i
++) {
70 if (!strcasecmp(string
, mappings
[i
].name
))
74 return mappings
[i
].value
;
79 get_facility(const char *option
)
82 strlcpy(facility
, option
, sizeof(facility
));
84 char *end
= strchr(facility
, '.');
86 // there is no facility specified
87 return DEFAULT_FACILITY
;
92 return lookup_mapping(sFacilities
, facility
);
97 get_priority(const char *option
)
99 char *priority
= strchr(option
, '.');
100 if (priority
== NULL
) {
101 // there is no facility specified
102 return DEFAULT_PRIORITY
;
105 return lookup_mapping(sPriorities
, ++priority
);
112 fprintf(stderr
, "usage: %s [-i] [-t <tag>] [-p <[facility.]priority>] "
114 "Sends a message to the system logging facility.\n"
115 "If <message> is omitted, the message is read from stdin.\n\n"
116 " -i\tAdds the team ID to the log.\n"
117 " -t\tSpecifies the tag under which this message is posted.\n"
118 " -p\tSets the facility and priority this is logged under.\n"
119 " \t<facility> can be one of:\n"
120 "\t\tkern, user, mail, daemon, auth, syslog, lpr, news,\n"
121 "\t\tuucp, cron, authpriv\n"
122 " \t<priority> can be one of:\n"
123 "\t\tdebug, info, notice, warning, warn, error, err,\n"
124 "\t\tcritical, crit,alert, panic, emerg.\n", sProgramName
);
131 main(int argc
, char **argv
)
133 struct passwd
* passwd
= getpwuid(geteuid());
134 const char* tag
= NULL
;
135 int facility
= DEFAULT_FACILITY
;
136 int priority
= DEFAULT_PRIORITY
;
142 while ((option
= getopt(argc
, argv
, "it:p:")) != -1) {
144 case 'i': // log team ID
152 case 'p': // facility/priority
153 facility
= get_facility(optarg
);
154 priority
= get_priority(optarg
);
163 if (tag
== NULL
&& passwd
!= NULL
)
164 tag
= passwd
->pw_name
;
167 argv
= &argv
[optind
];
169 openlog(tag
, options
, facility
);
172 // take message from arguments
177 for (int32 i
= 0; i
< argc
; i
++) {
178 int32 newLength
= length
+ strlen(argv
[i
]) + 1;
180 buffer
= (char *)realloc(buffer
, newLength
+ 1);
181 if (buffer
== NULL
) {
182 fprintf(stderr
, "%s: out of memory\n", sProgramName
);
186 strcpy(buffer
+ length
, argv
[i
]);
189 buffer
[length
- 1] = ' ';
192 if (length
> 1 && buffer
[length
- 2] != '\n') {
193 buffer
[length
- 1] = '\n';
194 buffer
[length
] = '\0';
196 buffer
[length
- 1] = '\0';
198 syslog(priority
, "%s", buffer
);
201 // read messages from stdin
204 while (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
205 syslog(priority
, "%s", buffer
);