Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New logger classes (used once in Application DTOR as test and example)

  • Loading branch information...
commit 20d4a80320f1ac46473c32d647e907cd17ab8585 1 parent 446736a
@jonocole jonocole authored
Showing with 97 additions and 2 deletions.
  1. +94 −0 includes/playdar/logger.h
  2. +3 −2 src/application.cpp
View
94 includes/playdar/logger.h
@@ -0,0 +1,94 @@
+/*
+ Playdar - music content resolver
+ Copyright (C) 2009 Richard Jones
+ Copyright (C) 2009 Last.fm Ltd.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef PLAYDAR_LOG_H_
+#define PLAYDAR_LOG_H_
+
+#include <map>
+#include <string>
+#include <fstream>
+#include <boost/shared_ptr.hpp>
+
+namespace playdar {
+namespace log {
+
+typedef boost::shared_ptr<std::ofstream> stream_ptr;
+template< class T > class base_log {
+public:
+ template<typename V> base_log& operator<<( const V& in ) {
+ *m_stream << " " << in;
+ return *this;
+ }
+
+ //Required to allow mutators to work (endl etc):
+ base_log&
+ operator<<( std::ostream& ( *pf )( std::ostream& ))
+ {
+ pf( *(m_stream) );
+ return *this;
+ }
+
+
+protected:
+ stream_ptr m_stream;
+ base_log( const std::string& filename = "playdar.log" ) {
+ //LOCK
+ if( s_streamMap.find( filename ) == s_streamMap.end() ||
+ !s_streamMap.find( filename )->second->is_open()) {
+
+ std::cout << "Opening file: " << filename << std::endl;
+ s_streamMap[ filename ] = stream_ptr( new std::ofstream( filename.c_str(), std::ios_base::out | std::ios_base::app ));
+ }
+
+ m_stream = s_streamMap[ filename ];
+
+ *m_stream << std::endl << T::logType() << ":";
+ //UNLOCK
+ }
+ static std::map< std::string, stream_ptr > s_streamMap;
+};
+
+template<class T>
+std::map< std::string, stream_ptr > base_log<T>::s_streamMap;
+
+class warning: public base_log< warning > {
+public:
+ warning(): base_log<warning>(){};
+ warning( const std::string& file ): base_log<warning>( file ){};
+ static const char* logType(){ return "WARNING";}
+};
+
+class error: public base_log< error > {
+public:
+ error(): base_log<error>(){};
+ error( const std::string& file ): base_log<error>( file ){};
+ static const char* logType(){ return "ERROR";}
+
+};
+
+class info: public base_log< info > {
+public:
+ info(): base_log<info>(){};
+ info( const std::string& file ): base_log<info>( file ){};
+ static const char* logType(){ return "INFO";}
+
+};
+
+}} // playdar::log
+
+#endif //PLAYDAR_LOG_H_
View
5 src/application.cpp
@@ -18,6 +18,7 @@
*/
#include "playdar/application.h"
#include "playdar/resolver.h"
+#include "playdar/logger.h"
#include <boost/program_options.hpp>
#include <boost/asio.hpp>
#include <playdar/playdar_request_handler.h>
@@ -35,8 +36,8 @@ MyApplication::MyApplication(Config c)
MyApplication::~MyApplication()
{
- cout << "DTOR MyApplication" << endl;
- cout << "Stopping resolver..." << endl;
+ log::info() << "DTOR MyApplication" << endl;
+ log::info() << "Stopping resolver..." << endl;
delete(m_resolver);
}
Please sign in to comment.
Something went wrong with that request. Please try again.