Keun young Park | 5eba08f | 2012-03-26 18:31:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | #include <stdio.h> |
| 17 | #include <stdarg.h> |
| 18 | |
| 19 | #include "StringUtil.h" |
| 20 | #include "Log.h" |
| 21 | |
| 22 | Log* Log::mInstance = NULL; |
| 23 | |
| 24 | #define ASSERT_PLAIN(cond) if(!(cond)) { fprintf(stderr, \ |
| 25 | "assertion failed %s %d", __FILE__, __LINE__); \ |
Dan Albert | 78691b4 | 2015-06-11 10:46:42 -0700 | [diff] [blame] | 26 | abort(); }; |
Keun young Park | 5eba08f | 2012-03-26 18:31:29 -0700 | [diff] [blame] | 27 | |
| 28 | Log* Log::Instance(const char* dirName) |
| 29 | { |
| 30 | if (!mInstance) { |
| 31 | mInstance = new Log(); |
| 32 | ASSERT_PLAIN(mInstance->init(dirName)); |
| 33 | } |
| 34 | return mInstance; |
| 35 | } |
| 36 | void Log::Finalize() |
| 37 | { |
| 38 | delete mInstance; |
| 39 | mInstance = NULL; |
| 40 | } |
| 41 | void Log::printf(LogLevel level, const char* fmt, ...) |
| 42 | { |
| 43 | va_list ap; |
| 44 | va_start(ap, fmt); |
| 45 | FileUtil::doVprintf(level < mLogLevel, level, fmt, ap); |
| 46 | va_end(ap); |
| 47 | } |
| 48 | |
| 49 | void Log::setLogLevel(LogLevel level) |
| 50 | { |
| 51 | mLogLevel = level; |
| 52 | } |
| 53 | |
| 54 | Log::Log() |
| 55 | : mLogLevel(ELogV) |
| 56 | { |
| 57 | ::fprintf(stderr, "Log level %d\n", mLogLevel); |
| 58 | } |
| 59 | |
| 60 | Log::~Log() |
| 61 | { |
| 62 | |
| 63 | } |
| 64 | |
| 65 | bool Log::init(const char* dirName) |
| 66 | { |
| 67 | if (dirName == NULL) { |
| 68 | return true; |
| 69 | } |
| 70 | android::String8 logFile; |
| 71 | if (logFile.appendFormat("%s/log.txt", dirName) != 0) { |
| 72 | return false; |
| 73 | } |
| 74 | return FileUtil::init(logFile.string()); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |