Home | History | Annotate | Download | only in apple
      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 "apple_glx_log.h"
     39 
     40 static bool diagnostic = false;
     41 static aslclient aslc;
     42 
     43 void apple_glx_log_init(void) {
     44     if (getenv("LIBGL_DIAGNOSTIC")) {
     45         diagnostic = true;
     46     }
     47 
     48     aslc = asl_open(NULL, NULL, 0);
     49 }
     50 
     51 void _apple_glx_log(int level, const char *file, const char *function,
     52                     int line, const char *fmt, ...) {
     53     va_list v;
     54     va_start(v, fmt);
     55     _apple_glx_vlog(level, file, function, line, fmt, v);
     56     va_end(v);
     57 }
     58 
     59 static const char *
     60 _asl_level_string(int level)
     61 {
     62         if (level == ASL_LEVEL_EMERG) return ASL_STRING_EMERG;
     63         if (level == ASL_LEVEL_ALERT) return ASL_STRING_ALERT;
     64         if (level == ASL_LEVEL_CRIT) return ASL_STRING_CRIT;
     65         if (level == ASL_LEVEL_ERR) return ASL_STRING_ERR;
     66         if (level == ASL_LEVEL_WARNING) return ASL_STRING_WARNING;
     67         if (level == ASL_LEVEL_NOTICE) return ASL_STRING_NOTICE;
     68         if (level == ASL_LEVEL_INFO) return ASL_STRING_INFO;
     69         if (level == ASL_LEVEL_DEBUG) return ASL_STRING_DEBUG;
     70         return "unknown";
     71 }
     72 
     73 void _apple_glx_vlog(int level, const char *file, const char *function,
     74                      int line, const char *fmt, va_list args) {
     75     aslmsg msg;
     76     uint64_t thread = 0;
     77 
     78     if (pthread_is_threaded_np()) {
     79 #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
     80         thread = (uint64_t)(uintptr_t)pthread_self();
     81 #elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060
     82         if (&pthread_threadid_np) {
     83             pthread_threadid_np(NULL, &thread);
     84         } else {
     85             thread = (uint64_t)(uintptr_t)pthread_self();
     86         }
     87 #else
     88         pthread_threadid_np(NULL, &thread);
     89 #endif
     90     }
     91 
     92     if (diagnostic) {
     93         va_list args2;
     94         va_copy(args2, args);
     95 
     96         fprintf(stderr, "%-9s %24s:%-4d %s(%"PRIu64"): ",
     97                 _asl_level_string(level), file, line, function, thread);
     98         vfprintf(stderr, fmt, args2);
     99     }
    100 
    101     msg = asl_new(ASL_TYPE_MSG);
    102     if (msg) {
    103         if (file)
    104             asl_set(msg, "File", file);
    105         if (function)
    106             asl_set(msg, "Function", function);
    107         if (line) {
    108             char *_line;
    109             asprintf(&_line, "%d", line);
    110             if (_line) {
    111                 asl_set(msg, "Line", _line);
    112                 free(_line);
    113             }
    114         }
    115         if (pthread_is_threaded_np()) {
    116             char *_thread;
    117             asprintf(&_thread, "%"PRIu64, thread);
    118             if (_thread) {
    119                 asl_set(msg, "Thread", _thread);
    120                 free(_thread);
    121             }
    122         }
    123     }
    124 
    125     asl_vlog(aslc, msg, level, fmt, args);
    126     if (msg)
    127         asl_free(msg);
    128 }
    129