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