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