blob: 1d2434a56098c8865ebcf8159785beca7750fdab [file] [log] [blame]
Keun young Park5eba08f2012-03-26 18:31:29 -07001/*
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
22Log* Log::mInstance = NULL;
23
24#define ASSERT_PLAIN(cond) if(!(cond)) { fprintf(stderr, \
25 "assertion failed %s %d", __FILE__, __LINE__); \
Dan Albert78691b42015-06-11 10:46:42 -070026 abort(); };
Keun young Park5eba08f2012-03-26 18:31:29 -070027
28Log* Log::Instance(const char* dirName)
29{
30 if (!mInstance) {
31 mInstance = new Log();
32 ASSERT_PLAIN(mInstance->init(dirName));
33 }
34 return mInstance;
35}
36void Log::Finalize()
37{
38 delete mInstance;
39 mInstance = NULL;
40}
41void 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
49void Log::setLogLevel(LogLevel level)
50{
51 mLogLevel = level;
52}
53
54Log::Log()
55 : mLogLevel(ELogV)
56{
57 ::fprintf(stderr, "Log level %d\n", mLogLevel);
58}
59
60Log::~Log()
61{
62
63}
64
65bool 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