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 
18 #ifndef _LOG4CXX_SPI_ERROR_HANDLER_H
19 #define _LOG4CXX_SPI_ERROR_HANDLER_H
20 
21 #if defined(_MSC_VER)
22 	#pragma warning ( push )
23 	#pragma warning ( disable: 4231 4251 4275 4786 )
24 #endif
25 
26 
27 #include <log4cxx/spi/optionhandler.h>
28 #include <log4cxx/helpers/exception.h>
29 #include <log4cxx/appender.h>
30 #include <log4cxx/spi/loggingevent.h>
31 
32 namespace log4cxx
33 {
34 namespace spi
35 {
36 class ErrorCode
37 {
38 	public:
39 		enum
40 		{
41 			GENERIC_FAILURE = 0,
42 			WRITE_FAILURE = 1,
43 			FLUSH_FAILURE = 2,
44 			CLOSE_FAILURE = 3,
45 			FILE_OPEN_FAILURE = 4,
46 			MISSING_LAYOUT = 5,
47 			ADDRESS_PARSE_FAILURE = 6
48 		};
49 };
50 
51 
52 /**
53 Appenders may delegate their error handling to
54 <code>ErrorHandlers</code>.
55 
56 <p>Error handling is a particularly tedious to get right because by
57 definition errors are hard to predict and to reproduce.
58 
59 
60 <p>Please take the time to contact the author in case you discover
61 that errors are not properly handled. You are most welcome to
62 suggest new error handling policies or criticize existing policies.
63 */
64 class LOG4CXX_EXPORT ErrorHandler : public virtual OptionHandler
65 {
66 	public:
DECLARE_ABSTRACT_LOG4CXX_OBJECT(ErrorHandler)67 		DECLARE_ABSTRACT_LOG4CXX_OBJECT(ErrorHandler)
68 		BEGIN_LOG4CXX_CAST_MAP()
69 		LOG4CXX_CAST_ENTRY(ErrorHandler)
70 		LOG4CXX_CAST_ENTRY(OptionHandler)
71 		END_LOG4CXX_CAST_MAP()
72 
73 		virtual ~ErrorHandler() {}
74 
75 		/**
76 		Add a reference to a logger to which the failing appender might
77 		be attached to. The failing appender will be searched and
78 		replaced only in the loggers you add through this method.
79 
80 		@param logger One of the loggers that will be searched for the failing
81 		appender in view of replacement.
82 		*/
83 		virtual void setLogger(const LoggerPtr& logger) = 0;
84 
85 
86 		/**
87 		Equivalent to the error(const String&, helpers::Exception&, int,
88 		spi::LoggingEvent&) with the the event parameteter set to
89 		null.
90 		*/
91 		virtual void error(const LogString& message, const std::exception& e,
92 			int errorCode) const = 0;
93 
94 		/**
95 		This method is normally used to just print the error message
96 		passed as a parameter.
97 		*/
98 		virtual void error(const LogString& message) const = 0;
99 
100 		/**
101 		This method is invoked to handle the error.
102 
103 		@param message The message assoicated with the error.
104 		@param e The Exption that was thrown when the error occured.
105 		@param errorCode The error code associated with the error.
106 		@param event The logging event that the failing appender is asked
107 		to log.
108 		*/
109 		virtual void error(const LogString& message, const std::exception& e,
110 			int errorCode, const LoggingEventPtr& event) const = 0;
111 
112 		/**
113 		Set the appender for which errors are handled. This method is
114 		usually called when the error handler is configured.
115 		*/
116 		virtual void setAppender(const AppenderPtr& appender) = 0;
117 
118 		/**
119 		Set the appender to fallback upon in case of failure.
120 		*/
121 		virtual void setBackupAppender(const AppenderPtr& appender) = 0;
122 };
123 
124 LOG4CXX_PTR_DEF(ErrorHandler);
125 }  //namespace spi
126 } //namespace log4cxx
127 
128 #if defined(_MSC_VER)
129 	#pragma warning ( pop )
130 #endif
131 
132 #endif //_LOG4CXX_SPI_ERROR_HANDLER_H
133