1af69d88dSmrg/*
2af69d88dSmrg * Copyright (c) 2012 Apple Inc.
3af69d88dSmrg *
4af69d88dSmrg * Permission is hereby granted, free of charge, to any person
5af69d88dSmrg * obtaining a copy of this software and associated documentation files
6af69d88dSmrg * (the "Software"), to deal in the Software without restriction,
7af69d88dSmrg * including without limitation the rights to use, copy, modify, merge,
8af69d88dSmrg * publish, distribute, sublicense, and/or sell copies of the Software,
9af69d88dSmrg * and to permit persons to whom the Software is furnished to do so,
10af69d88dSmrg * subject to the following conditions:
11af69d88dSmrg *
12af69d88dSmrg * The above copyright notice and this permission notice shall be
13af69d88dSmrg * included in all copies or substantial portions of the Software.
14af69d88dSmrg *
15af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16af69d88dSmrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17af69d88dSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18af69d88dSmrg * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19af69d88dSmrg * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20af69d88dSmrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21af69d88dSmrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22af69d88dSmrg * DEALINGS IN THE SOFTWARE.
23af69d88dSmrg *
24af69d88dSmrg * Except as contained in this notice, the name(s) of the above
25af69d88dSmrg * copyright holders shall not be used in advertising or otherwise to
26af69d88dSmrg * promote the sale, use or other dealings in this Software without
27af69d88dSmrg * prior written authorization.
28af69d88dSmrg */
29af69d88dSmrg
30af69d88dSmrg#include <sys/cdefs.h>
31af69d88dSmrg#include <asl.h>
32af69d88dSmrg#include <stdio.h>
33af69d88dSmrg#include <stdbool.h>
34af69d88dSmrg#include <stdint.h>
35af69d88dSmrg#include <stdlib.h>
36af69d88dSmrg#include <inttypes.h>
37af69d88dSmrg#include <pthread.h>
387ec681f3Smrg#include "glxclient.h"
39af69d88dSmrg#include "apple_glx_log.h"
4001e04c3fSmrg#include "util/debug.h"
41af69d88dSmrg
42af69d88dSmrgstatic aslclient aslc;
43af69d88dSmrg
44af69d88dSmrgvoid apple_glx_log_init(void) {
45af69d88dSmrg    aslc = asl_open(NULL, NULL, 0);
46af69d88dSmrg}
47af69d88dSmrg
48af69d88dSmrgvoid _apple_glx_log(int level, const char *file, const char *function,
49af69d88dSmrg                    int line, const char *fmt, ...) {
50af69d88dSmrg    va_list v;
51af69d88dSmrg    va_start(v, fmt);
52af69d88dSmrg    _apple_glx_vlog(level, file, function, line, fmt, v);
53af69d88dSmrg    va_end(v);
54af69d88dSmrg}
55af69d88dSmrg
56af69d88dSmrgstatic const char *
57af69d88dSmrg_asl_level_string(int level)
58af69d88dSmrg{
59af69d88dSmrg        if (level == ASL_LEVEL_EMERG) return ASL_STRING_EMERG;
60af69d88dSmrg        if (level == ASL_LEVEL_ALERT) return ASL_STRING_ALERT;
61af69d88dSmrg        if (level == ASL_LEVEL_CRIT) return ASL_STRING_CRIT;
62af69d88dSmrg        if (level == ASL_LEVEL_ERR) return ASL_STRING_ERR;
63af69d88dSmrg        if (level == ASL_LEVEL_WARNING) return ASL_STRING_WARNING;
64af69d88dSmrg        if (level == ASL_LEVEL_NOTICE) return ASL_STRING_NOTICE;
65af69d88dSmrg        if (level == ASL_LEVEL_INFO) return ASL_STRING_INFO;
66af69d88dSmrg        if (level == ASL_LEVEL_DEBUG) return ASL_STRING_DEBUG;
67af69d88dSmrg        return "unknown";
68af69d88dSmrg}
69af69d88dSmrg
70af69d88dSmrgvoid _apple_glx_vlog(int level, const char *file, const char *function,
71af69d88dSmrg                     int line, const char *fmt, va_list args) {
72af69d88dSmrg    aslmsg msg;
73af69d88dSmrg    uint64_t thread = 0;
74af69d88dSmrg
75af69d88dSmrg    if (pthread_is_threaded_np()) {
76af69d88dSmrg#if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
77af69d88dSmrg        thread = (uint64_t)(uintptr_t)pthread_self();
78af69d88dSmrg#elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060
79af69d88dSmrg        if (&pthread_threadid_np) {
80af69d88dSmrg            pthread_threadid_np(NULL, &thread);
81af69d88dSmrg        } else {
82af69d88dSmrg            thread = (uint64_t)(uintptr_t)pthread_self();
83af69d88dSmrg        }
84af69d88dSmrg#else
85af69d88dSmrg        pthread_threadid_np(NULL, &thread);
86af69d88dSmrg#endif
87af69d88dSmrg    }
88af69d88dSmrg
897ec681f3Smrg    DebugMessageF("%-9s %24s:%-4d %s(%"PRIu64"): ",
907ec681f3Smrg                  _asl_level_string(level), file, line, function, thread);
91af69d88dSmrg
92af69d88dSmrg    msg = asl_new(ASL_TYPE_MSG);
93af69d88dSmrg    if (msg) {
94af69d88dSmrg        if (file)
95af69d88dSmrg            asl_set(msg, "File", file);
96af69d88dSmrg        if (function)
97af69d88dSmrg            asl_set(msg, "Function", function);
98af69d88dSmrg        if (line) {
99af69d88dSmrg            char *_line;
100af69d88dSmrg            asprintf(&_line, "%d", line);
101af69d88dSmrg            if (_line) {
102af69d88dSmrg                asl_set(msg, "Line", _line);
103af69d88dSmrg                free(_line);
104af69d88dSmrg            }
105af69d88dSmrg        }
106af69d88dSmrg        if (pthread_is_threaded_np()) {
107af69d88dSmrg            char *_thread;
108af69d88dSmrg            asprintf(&_thread, "%"PRIu64, thread);
109af69d88dSmrg            if (_thread) {
110af69d88dSmrg                asl_set(msg, "Thread", _thread);
111af69d88dSmrg                free(_thread);
112af69d88dSmrg            }
113af69d88dSmrg        }
114af69d88dSmrg    }
115af69d88dSmrg
116af69d88dSmrg    asl_vlog(aslc, msg, level, fmt, args);
117af69d88dSmrg    if (msg)
118af69d88dSmrg        asl_free(msg);
119af69d88dSmrg}
120