+
+void
+plogsetlevelstr(char *levelstr)
+{
+ if (!levelstr) {
+ return;
+ }
+
+ if (strncmp(levelstr, ASL_STRING_EMERG, sizeof(ASL_STRING_EMERG) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_EMERG);
+ } else if (strncmp(levelstr, ASL_STRING_ALERT, sizeof(ASL_STRING_ALERT) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_ALERT);
+ } else if (strncmp(levelstr, ASL_STRING_CRIT, sizeof(ASL_STRING_CRIT) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_CRIT);
+ } else if (strncmp(levelstr, ASL_STRING_ERR, sizeof(ASL_STRING_ERR) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_ERR);
+ } else if (strncmp(levelstr, ASL_STRING_WARNING, sizeof(ASL_STRING_NOTICE) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_WARNING);
+ } else if (strncmp(levelstr, ASL_STRING_NOTICE, sizeof(ASL_STRING_NOTICE) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_NOTICE);
+ } else if (strncmp(levelstr, ASL_STRING_INFO, sizeof(ASL_STRING_INFO) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_INFO);
+ } else if (strncmp(levelstr, ASL_STRING_DEBUG, sizeof(ASL_STRING_DEBUG) - 1) == 0) {
+ plogsetlevel(ASL_LEVEL_DEBUG);
+ }
+}
+
+void
+plogsetlevelquotedstr (char *levelquotedstr)
+{
+ int len;
+
+ if (!levelquotedstr) {
+ plog(ASL_LEVEL_ERR, "Null log level (quoted string)");
+ return;
+ }
+
+ len = strlen(levelquotedstr);
+ if (len < 3 ||
+ levelquotedstr[0] != '"' ||
+ levelquotedstr[len - 1] != '"') {
+ plog(ASL_LEVEL_ERR, "Invalid log level (quoted string): %s", levelquotedstr);
+ return;
+ }
+ // skip quotes
+ levelquotedstr[len - 1] = '\0';
+ plogsetlevelstr(&levelquotedstr[1]);
+}
+
+void
+plogreadprefs (void)
+{
+ CFPropertyListRef globals;
+ CFStringRef logFileRef;
+ CFNumberRef debugLevelRef;
+ CFStringRef debugLevelStringRef;
+ char logLevelStr[16];
+ int level = 0;
+
+ logLevelStr[0] = 0;
+
+ SCPreferencesSynchronize(gPrefs);
+
+ globals = SCPreferencesGetValue(gPrefs, CFSTR("Global"));
+ if (!globals || (CFGetTypeID(globals) != CFDictionaryGetTypeID())) {
+ return;
+ }
+ debugLevelRef = CFDictionaryGetValue(globals, CFSTR("DebugLevel"));
+ if (debugLevelRef && (CFGetTypeID(debugLevelRef) == CFNumberGetTypeID())) {
+ CFNumberGetValue(debugLevelRef, kCFNumberSInt32Type, &level);
+ plogsetlevel(level);
+ } else {
+ debugLevelStringRef = CFDictionaryGetValue(globals, CFSTR("DebugLevelString"));
+ if (debugLevelStringRef && (CFGetTypeID(debugLevelStringRef) == CFStringGetTypeID())) {
+ CFStringGetCString(debugLevelStringRef, logLevelStr, sizeof(logLevelStr), kCFStringEncodingMacRoman);
+ plogsetlevelstr(logLevelStr);
+ }
+ }
+
+ logFileRef = CFDictionaryGetValue(globals, CFSTR("DebugLogfile"));
+ if (!logFileRef || (CFGetTypeID(logFileRef) != CFStringGetTypeID())) {
+ return;
+ }
+ CFStringGetCString(logFileRef, logFileStr, MAXPATHLEN, kCFStringEncodingMacRoman);
+ plogsetfile(logFileStr);
+}
+
+void
+ploginit(void)
+{
+ logFileStr[0] = 0;
+ logRef = NULL;//asl_open(NULL, plog_facility, 0);
+ plogsetlevel(ASL_LEVEL_NOTICE);
+ //plogsetlevel(ASL_LEVEL_DEBUG);
+ plogreadprefs();
+}
+
+void
+plogsetsessioninfo (const char *session_id,
+ const char *session_type,
+ const char *session_ver)
+{
+ if (gSessId) {
+ free(gSessId);
+ }
+ if (!session_id) {
+ gSessId = NULL;
+ } else {
+ gSessId = strdup(session_id);
+ }
+ if (gSessId) {
+ free(gSessId);
+ }
+ if (!session_type) {
+ gSessType = NULL;
+ } else {
+ gSessType = strdup(session_id);
+ }
+ if (gSessVer) {
+ free(gSessVer);
+ }
+ if (!session_ver) {
+ gSessVer = NULL;
+ } else {
+ gSessVer = strdup(session_ver);
+ }
+}
+
+char *
+createCStringFromCFString(CFAllocatorRef allocator, CFStringRef cfstr)
+{
+ CFIndex cstr_len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr), kCFStringEncodingUTF8) + 1;
+ char *cstr = (char *)CFAllocatorAllocate(allocator, cstr_len, 0);
+ CFStringGetCString(cfstr, cstr, cstr_len, kCFStringEncodingUTF8);
+ return cstr;
+}
+
+void
+plogcf(int priority, CFStringRef fmt, ...)
+{
+ va_list args;
+ CFStringRef cfstr;
+ char *cstr;
+
+ va_start(args, fmt);
+ cfstr = CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, NULL, fmt, args);
+ va_end(args);
+
+ cstr = createCStringFromCFString(kCFAllocatorDefault, cfstr);
+ plog(priority, "%s", cstr);
+
+ CFAllocatorDeallocate(kCFAllocatorDefault, cstr);
+ CFRelease(cfstr);
+}
+
+