1// Copyright (c) 2012 - Cloud Instruments Co., Ltd.
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this
9//    list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11//    this list of conditions and the following disclaimer in the documentation
12//    and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25package seelog
26
27// Log level type
28type LogLevel uint8
29
30// Log levels
31const (
32	TraceLvl = iota
33	DebugLvl
34	InfoLvl
35	WarnLvl
36	ErrorLvl
37	CriticalLvl
38	Off
39)
40
41// Log level string representations (used in configuration files)
42const (
43	TraceStr    = "trace"
44	DebugStr    = "debug"
45	InfoStr     = "info"
46	WarnStr     = "warn"
47	ErrorStr    = "error"
48	CriticalStr = "critical"
49	OffStr      = "off"
50)
51
52var levelToStringRepresentations = map[LogLevel]string{
53	TraceLvl:    TraceStr,
54	DebugLvl:    DebugStr,
55	InfoLvl:     InfoStr,
56	WarnLvl:     WarnStr,
57	ErrorLvl:    ErrorStr,
58	CriticalLvl: CriticalStr,
59	Off:         OffStr,
60}
61
62// LogLevelFromString parses a string and returns a corresponding log level, if sucessfull.
63func LogLevelFromString(levelStr string) (level LogLevel, found bool) {
64	for lvl, lvlStr := range levelToStringRepresentations {
65		if lvlStr == levelStr {
66			return lvl, true
67		}
68	}
69
70	return 0, false
71}
72
73// LogLevelToString returns seelog string representation for a specified level. Returns "" for invalid log levels.
74func (level LogLevel) String() string {
75	levelStr, ok := levelToStringRepresentations[level]
76	if ok {
77		return levelStr
78	}
79
80	return ""
81}
82