+static void
+_asl_set_option(asl_msg_t *msg, const char *opt)
+{
+ const char *val = NULL;
+ uint32_t status;
+
+ if (msg == NULL) return;
+ if (opt == NULL) return;
+
+ status = asl_msg_lookup(msg, ASL_KEY_OPTION, &val, NULL);
+ if ((status != 0) || (val == NULL))
+ {
+ asl_msg_set_key_val(msg, ASL_KEY_OPTION, opt);
+ }
+ else
+ {
+ char *option = NULL;
+ asprintf(&option, "%s %s", opt, val);
+ asl_msg_set_key_val(msg, ASL_KEY_OPTION, option);
+ free(option);
+ }
+}
+
+static ASL_STATUS
+_asl_send_message_text(asl_client_t *asl, asl_msg_t *sendmsg, asl_object_t obj, uint32_t eval, asl_msg_t *msg, const char *mstr, bool shimmed)
+{
+ int status;
+ uint32_t level, lmask;
+ asl_msg_t *release_sendmsg = NULL;
+
+ if (asl == NULL) {
+ if (obj == NULL) {
+ asl = _asl_open_default();
+ if (asl == NULL) return ASL_STATUS_FAILED;
+ } else {
+ uint32_t objtype = asl_get_type(obj);
+ if (objtype == ASL_TYPE_CLIENT) asl = (asl_client_t *)obj;
+ }
+ }
+
+ level = eval & EVAL_LEVEL_MASK;
+ if (level > 7) level = 7;
+ lmask = ASL_FILTER_MASK(level);
+
+ if (sendmsg == NULL) {
+ struct timeval tval = {0, 0};
+ const char *sstr, *fstr;
+
+ status = gettimeofday(&tval, NULL);
+ if (status != 0) {
+ time_t tick = time(NULL);
+ tval.tv_sec = tick;
+ tval.tv_usec = 0;
+ }
+
+ sstr = NULL;
+ status = asl_msg_lookup(msg, ASL_KEY_SENDER, &sstr, NULL);
+ if (status != 0) {
+ sstr = NULL;
+ }
+
+ fstr = NULL;
+ status = asl_msg_lookup(msg, ASL_KEY_FACILITY, &fstr, NULL);
+ if (status != 0) {
+ fstr = NULL;
+ }
+ sendmsg = asl_base_msg(asl, level, &tval, sstr, fstr, mstr);
+ if (sendmsg == NULL) {
+ return ASL_STATUS_FAILED;
+ }
+
+ release_sendmsg = sendmsg = asl_msg_merge(sendmsg, msg);
+ }
+
+ /* write to file descriptors */
+ for (uint32_t i = 0; i < asl->out_count; i++) {
+ if (shimmed) {
+ if ((asl->out_list[i].fd != STDOUT_FILENO) && (asl->out_list[i].fd != STDERR_FILENO)) {
+ continue; // new logging only support stdout/stderr
+ }
+ }
+
+ if ((asl->out_list[i].fd >= 0) && (asl->out_list[i].filter != 0) && ((asl->out_list[i].filter & lmask) != 0)) {
+ char *str;
+
+ uint32_t len = 0;
+ str = asl_format_message(sendmsg, asl->out_list[i].mfmt, asl->out_list[i].tfmt, asl->out_list[i].encoding, &len);
+ if (str == NULL) continue;
+
+ status = write(asl->out_list[i].fd, str, len - 1);
+ if (status < 0)
+ {
+ /* soft error for fd 2 (stderr) */
+ if (asl->out_list[i].fd == 2) status = 0;
+ asl->out_list[i].fd = -1;
+ } else {
+ status = 0;
+ }
+
+ free(str);
+ }
+ }
+ if (release_sendmsg) {
+ asl_msg_release(release_sendmsg);
+ }
+
+ return status;
+}
+