| 1 | |
| 2 | /* |
| 3 | * Copyright (c) 2013 Thomas Perl <m@thp.io> |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | #include "logging.h" |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <pthread.h> |
| 26 | |
| 27 | FILE *hybris_logging_target = NULL; |
| 28 | |
| 29 | pthread_mutex_t hybris_logging_mutex; |
| 30 | |
| 31 | static enum hybris_log_level |
| 32 | hybris_minimum_log_level = HYBRIS_LOG_WARN; |
| 33 | |
| 34 | static enum hybris_log_format _hybris_logging_format = HYBRIS_LOG_FORMAT_NORMAL; |
| 35 | |
| 36 | static int _hybris_should_trace = 0; |
| 37 | |
| 38 | static int |
| 39 | hybris_logging_initialized = 0; |
| 40 | |
| 41 | static void |
| 42 | hybris_logging_initialize() |
| 43 | { |
| 44 | const char *env = getenv("HYBRIS_LOGGING_LEVEL"); |
| 45 | |
| 46 | if (env == NULL) { |
| 47 | /* Nothing to do - use default level */ |
| 48 | } else if (strcmp(env, "debug") == 0) { |
| 49 | hybris_minimum_log_level = HYBRIS_LOG_DEBUG; |
| 50 | } else if (strcmp(env, "info") == 0) { |
| 51 | hybris_minimum_log_level = HYBRIS_LOG_INFO; |
| 52 | } else if (strcmp(env, "warn") == 0) { |
| 53 | hybris_minimum_log_level = HYBRIS_LOG_WARN; |
| 54 | } else if (strcmp(env, "error") == 0) { |
| 55 | hybris_minimum_log_level = HYBRIS_LOG_ERROR; |
| 56 | } else if (strcmp(env, "disabled") == 0) { |
| 57 | hybris_minimum_log_level = HYBRIS_LOG_DISABLED; |
| 58 | } |
| 59 | |
| 60 | env = getenv("HYBRIS_LOGGING_TARGET"); |
| 61 | if (env != NULL) |
| 62 | { |
| 63 | hybris_logging_target = fopen(env, "a"); |
| 64 | } |
| 65 | if (hybris_logging_target == NULL) |
| 66 | hybris_logging_target = stderr; |
| 67 | |
| 68 | env = getenv("HYBRIS_LOGGING_FORMAT"); |
| 69 | if (env != NULL) |
| 70 | { |
| 71 | if (strcmp(env, "systrace") == 0) { |
| 72 | _hybris_logging_format = HYBRIS_LOG_FORMAT_SYSTRACE; |
| 73 | } |
| 74 | else |
| 75 | _hybris_logging_format = HYBRIS_LOG_FORMAT_NORMAL; |
| 76 | } |
| 77 | |
| 78 | env = getenv("HYBRIS_TRACE"); |
| 79 | if (env != NULL) |
| 80 | { |
| 81 | if (strcmp(env, "1") == 0) { |
| 82 | _hybris_should_trace = 1; |
| 83 | } |
| 84 | } |
| 85 | pthread_mutex_init(&hybris_logging_mutex, NULL); |
| 86 | } |
| 87 | |
| 88 | int |
| 89 | hybris_should_log(enum hybris_log_level level) |
| 90 | { |
| 91 | /* Initialize logging level from environment */ |
| 92 | if (!hybris_logging_initialized) { |
| 93 | hybris_logging_initialized = 1; |
| 94 | hybris_logging_initialize(); |
| 95 | } |
| 96 | |
| 97 | return (level >= hybris_minimum_log_level); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | hybris_set_log_level(enum hybris_log_level level) |
| 102 | { |
| 103 | hybris_minimum_log_level = level; |
| 104 | } |
| 105 | |
| 106 | void * |
| 107 | hybris_get_thread_id() |
| 108 | { |
| 109 | return (void *)pthread_self(); |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | hybris_should_trace(const char *module, const char *tracepoint) |
| 114 | { |
| 115 | return _hybris_should_trace; |
| 116 | } |
| 117 | |
| 118 | enum hybris_log_format hybris_logging_format() |
| 119 | { |
| 120 | return _hybris_logging_format; |
| 121 | } |