github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/lib/log_callback.cc (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see 17 // <http://www.gnu.org/licenses/>. 18 // 19 20 #include "log_callback.h" 21 #include "logger.h" 22 23 #include <stdarg.h> 24 25 static LogFunc LOG = NULL; 26 static const char *LogLevelText[] = {"DEBUG", "WARN", "INFO", "ERROR"}; 27 28 const char *GetLogLevelText(int level) { 29 if (level > LogLevel::ERROR) { 30 level = LogLevel::ERROR; 31 } else if (level < LogLevel::DEBUG) { 32 level = LogLevel::INFO; 33 } 34 35 return LogLevelText[level - 1]; 36 }; 37 38 void InitializeLogger(LogFunc log) { LOG = log; } 39 40 void NewNativeLogFunction(Isolate *isolate, Local<ObjectTemplate> globalTpl) { 41 globalTpl->Set(String::NewFromUtf8(isolate, "_native_log"), 42 FunctionTemplate::New(isolate, LogCallback), 43 static_cast<PropertyAttribute>(PropertyAttribute::DontDelete | 44 PropertyAttribute::ReadOnly)); 45 } 46 47 void LogCallback(const FunctionCallbackInfo<Value> &info) { 48 Isolate *isolate = info.GetIsolate(); 49 if (info.Length() < 2) { 50 isolate->ThrowException(Exception::Error( 51 String::NewFromUtf8(isolate, "_native_log: mssing params"))); 52 return; 53 } 54 55 Local<Value> level = info[0]; 56 if (!level->IsNumber()) { 57 isolate->ThrowException(Exception::Error( 58 String::NewFromUtf8(isolate, "_native_log: level must be number"))); 59 return; 60 } 61 62 Local<Value> msg = info[1]; 63 if (!msg->IsString()) { 64 isolate->ThrowException(Exception::Error( 65 String::NewFromUtf8(isolate, "_native_log: msg must be string"))); 66 return; 67 } 68 69 if (LOG == NULL) { 70 return; 71 } 72 73 String::Utf8Value m(msg); 74 LOG((level->ToInt32())->Int32Value(), *m); 75 } 76 77 void LogInfof(const char *format, ...) { 78 if (LOG == NULL) { 79 return; 80 } 81 82 va_list vl; 83 va_start(vl, format); 84 85 char *msg = NULL; 86 vasprintf(&msg, format, vl); 87 if (msg != NULL) { 88 LOG(LogLevel::INFO, msg); 89 free(msg); 90 } 91 92 va_end(vl); 93 } 94 95 void LogErrorf(const char *format, ...) { 96 if (LOG == NULL) { 97 return; 98 } 99 100 va_list vl; 101 va_start(vl, format); 102 103 char *msg = NULL; 104 vasprintf(&msg, format, vl); 105 if (msg != NULL) { 106 LOG(LogLevel::ERROR, msg); 107 free(msg); 108 } 109 110 va_end(vl); 111 } 112 113 void LogWarnf(const char *format, ...) { 114 if (LOG == NULL) { 115 return; 116 } 117 118 va_list vl; 119 va_start(vl, format); 120 121 char *msg = NULL; 122 vasprintf(&msg, format, vl); 123 if (msg != NULL) { 124 LOG(LogLevel::WARN, msg); 125 free(msg); 126 } 127 128 va_end(vl); 129 } 130 131 void LogDebugf(const char *format, ...) { 132 if (LOG == NULL) { 133 return; 134 } 135 136 va_list vl; 137 va_start(vl, format); 138 139 char *msg = NULL; 140 vasprintf(&msg, format, vl); 141 if (msg != NULL) { 142 LOG(LogLevel::DEBUG, msg); 143 free(msg); 144 } 145 146 va_end(vl); 147 } 148 149 void LogFatalf(const char *format, ...) { 150 if (LOG == NULL) { 151 return; 152 } 153 154 va_list vl; 155 va_start(vl, format); 156 157 char *msg = NULL; 158 vasprintf(&msg, format, vl); 159 if (msg != NULL) { 160 LOG(LogLevel::ERROR, msg); 161 free(msg); 162 } 163 164 va_end(vl); 165 }