1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #if defined(_MSC_VER)
18 	#pragma warning ( disable: 4231 4251 4275 4786 )
19 #endif
20 
21 #include <log4cxx/logstring.h>
22 #include <log4cxx/pattern/levelpatternconverter.h>
23 #include <log4cxx/spi/loggingevent.h>
24 #include <log4cxx/spi/location/locationinfo.h>
25 #include <log4cxx/level.h>
26 
27 
28 using namespace log4cxx;
29 using namespace log4cxx::pattern;
30 using namespace log4cxx::spi;
31 using namespace log4cxx::helpers;
32 
IMPLEMENT_LOG4CXX_OBJECT(LevelPatternConverter)33 IMPLEMENT_LOG4CXX_OBJECT(LevelPatternConverter)
34 
35 LevelPatternConverter::LevelPatternConverter() :
36 	LoggingEventPatternConverter(LOG4CXX_STR("Level"),
37 		LOG4CXX_STR("level"))
38 {
39 }
40 
newInstance(const std::vector<LogString> &)41 PatternConverterPtr LevelPatternConverter::newInstance(
42 	const std::vector<LogString>& /* options */)
43 {
44 	static PatternConverterPtr def(new LevelPatternConverter());
45 	return def;
46 }
47 
format(const LoggingEventPtr & event,LogString & toAppendTo,log4cxx::helpers::Pool &) const48 void LevelPatternConverter::format(
49 	const LoggingEventPtr& event,
50 	LogString& toAppendTo,
51 	log4cxx::helpers::Pool& /* p */) const
52 {
53 	toAppendTo.append(event->getLevel()->toString());
54 }
55 
56 
57 /**
58  * {@inheritDoc}
59  */
getStyleClass(const ObjectPtr & obj) const60 LogString LevelPatternConverter::getStyleClass(const ObjectPtr& obj) const
61 {
62 	LoggingEventPtr e(obj);
63 
64 	if (e != NULL)
65 	{
66 		int lint = e->getLevel()->toInt();
67 
68 		switch (lint)
69 		{
70 			case Level::TRACE_INT:
71 				return LOG4CXX_STR("level trace");
72 
73 			case Level::DEBUG_INT:
74 				return LOG4CXX_STR("level debug");
75 
76 			case Level::INFO_INT:
77 				return LOG4CXX_STR("level info");
78 
79 			case Level::WARN_INT:
80 				return LOG4CXX_STR("level warn");
81 
82 			case Level::ERROR_INT:
83 				return LOG4CXX_STR("level error");
84 
85 			case Level::FATAL_INT:
86 				return LOG4CXX_STR("level fatal");
87 
88 			default:
89 				return LogString(LOG4CXX_STR("level ")) + e->getLevel()->toString();
90 		}
91 	}
92 
93 	return LOG4CXX_STR("level");
94 }
95