/* * Copyright 2003,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef _LOG4CXX_NET_SMTP_H #define _LOG4CXX_NET_SMTP_H #include #ifdef HAVE_SMTP #include #include #include namespace log4cxx { namespace net { class SMTPAppender; typedef helpers::ObjectPtrT SMTPAppenderPtr; /** Send an e-mail when a specific logging event occurs, typically on errors or fatal errors.

The number of logging events delivered in this e-mail depend on the value of BufferSize option. The SMTPAppender keeps only the last BufferSize logging events in its cyclic buffer. This keeps memory requirements at a reasonable level while still delivering useful application context. */ class LOG4CXX_EXPORT SMTPAppender : public AppenderSkeleton { private: String to; String from; String subject; String smtpHost; int bufferSize; // 512 bool locationInfo; helpers::CyclicBuffer cb; void * session; String encoding; String charset; protected: spi::TriggeringEventEvaluatorPtr evaluator; public: DECLARE_LOG4CXX_OBJECT(SMTPAppender) BEGIN_LOG4CXX_CAST_MAP() LOG4CXX_CAST_ENTRY(SMTPAppender) LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton) END_LOG4CXX_CAST_MAP() /** The default constructor will instantiate the appender with a spi::TriggeringEventEvaluator that will trigger on events with level ERROR or higher.*/ SMTPAppender(); /** Use evaluator passed as parameter as the spi::TriggeringEventEvaluator for this net::SMTPAppender. */ SMTPAppender(spi::TriggeringEventEvaluatorPtr evaluator); ~SMTPAppender(); /** Set options */ virtual void setOption(const String& option, const String& value); /** Activate the specified options, such as the smtp host, the recipient, from, etc. */ virtual void activateOptions(); /** Perform SMTPAppender specific appending actions, mainly adding the event to a cyclic buffer and checking if the event triggers an e-mail to be sent. */ virtual void append(const spi::LoggingEventPtr& event); /** This method determines if there is a sense in attempting to append.

It checks whether there is a set output target and also if there is a set layout. If these checks fail, then the boolean value false is returned. */ bool checkEntryConditions(); virtual void close(); std::vector parseAddress(const String& addressStr); /** Returns value of the To option. */ inline const String& getTo() const { return to; } /** The SMTPAppender requires a {@link Layout layout}. */ virtual bool requiresLayout() const { return true; } /** Send the contents of the cyclic buffer as an e-mail message. */ void sendBuffer(); /** Returns value of the Charset option. */ inline const String& getCharset() const { return charset; } /** Returns value of the Encoding option. */ inline const String& getEncoding() const { return encoding; } /** Returns value of the EvaluatorClass option. */ String getEvaluatorClass(); /** Returns value of the From option. */ inline const String& getFrom() const { return from; } /** Returns value of the Subject option. */ inline const String& getSubject() const { return subject; } /** The Charset option takes a string value which should be the charset of the mail (us-ascii, iso8859_1, iso8859_2, iso8859_3). */ inline void setCharset(const String& charset) { this->charset = charset; } /** The Encoding option takes a string value which should be the encoding type of the mail (7bit, 8bit, base64, binary, quoted). */ inline void setEncoding(const String& charset) { this->encoding = encoding; } /** The From option takes a string value which should be a e-mail address of the sender. */ inline void setFrom(const String& from) { this->from = from; } /** The Subject option takes a string value which should be a the subject of the e-mail message. */ inline void setSubject(const String& subject) { this->subject = subject; } /** The BufferSize option takes a positive integer representing the maximum number of logging events to collect in a cyclic buffer. When the BufferSize is reached, oldest events are deleted as new events are added to the buffer. By default the size of the cyclic buffer is 512 events. */ void setBufferSize(int bufferSize); /** The SMTPHost option takes a string value which should be a the host name of the SMTP server that will send the e-mail message. */ inline void setSMTPHost(const String& smtpHost) { this->smtpHost = smtpHost; } /** Returns value of the SMTPHost option. */ inline const String& getSMTPHost() const { return smtpHost; } /** The To option takes a string value which should be a comma separated list of e-mail address of the recipients. */ inline void setTo(const String& to) { this->to = to; } /** Returns value of the BufferSize option. */ inline int getBufferSize() const { return bufferSize; } /** The EvaluatorClass option takes a string value representing the name of the class implementing the spi::TriggeringEventEvaluator interface. A corresponding object will be instantiated and assigned as the triggering event evaluator for the SMTPAppender. */ void setEvaluatorClass(const String& value); /** The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no effort to extract the location information related to the event. As a result, the layout that formats the events as they are sent out in an e-mail is likely to place the wrong location information (if present in the format).

Location information extraction is comparatively very slow and should be avoided unless performance is not a concern. */ inline void setLocationInfo(bool locationInfo) { this->locationInfo = locationInfo; } /** Returns value of the LocationInfo option. */ inline bool getLocationInfo() const { return locationInfo; } }; // class SMTPAppender class LOG4CXX_EXPORT DefaultEvaluator : public virtual spi::TriggeringEventEvaluator, public virtual helpers::ObjectImpl { public: DECLARE_LOG4CXX_OBJECT(DefaultEvaluator) BEGIN_LOG4CXX_CAST_MAP() LOG4CXX_CAST_ENTRY(spi::TriggeringEventEvaluator) END_LOG4CXX_CAST_MAP() /** Is this event the e-mail triggering event?

This method returns true, if the event level has ERROR level or higher. Otherwise it returns false. */ virtual bool isTriggeringEvent(const spi::LoggingEventPtr& event); }; // class DefaultEvaluator } // namespace net }; // namespace log4cxx #endif // HAVE_SMTP #endif // _LOG4CXX_NET_SMTP_H