xref: /haiku/src/apps/haikudepot/model/Logger.cpp (revision 13581b3d2a71545960b98fefebc5225b5bf29072)
1 /*
2  * Copyright 2017-2020, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "Logger.h"
6 
7 
8 log_level Logger::fLevel = LOG_LEVEL_INFO;
9 
10 
11 log_level
12 Logger::Level()
13 {
14 	return fLevel;
15 }
16 
17 
18 void
19 Logger::SetLevel(log_level value)
20 {
21 	fLevel = value;
22 }
23 
24 
25 /*static*/
26 const char*
27 Logger::NameForLevel(log_level value)
28 {
29 	switch (value) {
30 		case LOG_LEVEL_OFF:
31 			return "off";
32 		case LOG_LEVEL_INFO:
33 			return "info";
34 		case LOG_LEVEL_DEBUG:
35 			return "debug";
36 		case LOG_LEVEL_TRACE:
37 			return "trace";
38 		case LOG_LEVEL_ERROR:
39 			return "error";
40 		default:
41 			return "?";
42 	}
43 }
44 
45 
46 /*static*/ bool
47 Logger::SetLevelByName(const char *name)
48 {
49 	if (strcmp(name, "off") == 0) {
50 		fLevel = LOG_LEVEL_OFF;
51 	} else if (strcmp(name, "info") == 0) {
52 		fLevel = LOG_LEVEL_INFO;
53 	} else if (strcmp(name, "debug") == 0) {
54 		fLevel = LOG_LEVEL_DEBUG;
55 	} else if (strcmp(name, "trace") == 0) {
56 		fLevel = LOG_LEVEL_TRACE;
57 	} else if (strcmp(name, "error") == 0) {
58 		fLevel = LOG_LEVEL_ERROR;
59 	} else {
60 		return false;
61 	}
62 
63 	return true;
64 }
65 
66 
67 /*static*/
68 bool
69 Logger::IsLevelEnabled(log_level value)
70 {
71 	return fLevel >= value;
72 }
73 
74 
75 bool
76 Logger::IsInfoEnabled()
77 {
78 	return IsLevelEnabled(LOG_LEVEL_INFO);
79 }
80 
81 
82 bool
83 Logger::IsDebugEnabled()
84 {
85 	return IsLevelEnabled(LOG_LEVEL_DEBUG);
86 }
87 
88 
89 bool
90 Logger::IsTraceEnabled()
91 {
92 	return IsLevelEnabled(LOG_LEVEL_TRACE);
93 }
94 
95 
96 /*! Returns a small integer that indicates the current thread identifier. This
97 	is to _approximately_ be able to distinguish between threads in log lines.
98 */
99 
100 /*static*/ uint32
101 Logger::CurrentThreadIndicator()
102 {
103 	uint32 thread_id_abs = static_cast<uint32>(find_thread(NULL));
104 	return thread_id_abs % 1000;
105 }
106