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