473,387 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

custom logger allowing streams and logging to file or screen or both

1
Dear all,
I would like to create a custom logger which allows logging commands in stream-style, but at the same time the output goes to some file or to the screen (cout) or to both. The code below illustrates the functionality:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include "logger.hpp"
  3. using std::endl;
  4.  
  5. int main()
  6. {
  7.   Log log;
  8.   log.init("out.log");
  9.   log.setLogOutputToFile(true);
  10.   log.setLogOutputToScreen(true);
  11.  
  12.   int i = 10;
  13.   log << "iteration #" << i << endl;
  14.   return 0;
  15. }
Most of the member functions above are trivial to implement. I tried to overload operator<< as follows:

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. Log& Log::operator<<( T& os )
  3. {
  4.   if (printToFile_)
  5.   {
  6.     logFile_ << os;
  7.   }
  8.  
  9.   if (printToScreen_)
  10.   {
  11.     cout << os;
  12.   }
  13.  
  14.   return *this;
  15. }
  16.  
After compiling with g++ I get the following errors:
src/main.cpp:17: error: no match for ‘operator<<’ in ‘((Log*)log.Log::operator<< [with T = const char [12]](((const char (&)[12])"iteration #")))->Log::operator<< [with T = int](((int&)(& i))) << std::endl’
or compiling with intel's-icpc
src/main.cpp(17): error: no operator "<<" matches these operands
operand types are: Log << <unknown-type>
log << "iteration #" << i << endl;
Not to mention that we do not only desire to allow "endl" but all manipulators like "std::right, std::left, setw" etc.

Is there something I am doing wrong? Of course there is, but what can I do to get rid of these error messages?

Thank you all for your time,
Archwn.
Sep 23 '09 #1
0 1578

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nermin Brgulja | last post by:
Hi, I am using jdk1.4 Logger with a FileHandler for logging in a web application, and have two questions regarding logging: The first question is: I've created an Logger and mapped it to an...
3
by: Andreas Jung | last post by:
I am trying to write a custom logger with a custom handler: class MyHandler(StreamHandler): pass class Logger: def __init__(self): self._l = logging.getLogger('someident')...
9
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is...
1
by: Alex Honold | last post by:
Hi, I am involved right now in a project where I have to deliver a shell logging mechanism (for solaris). The requirements where not easy to fullfill already at the design level, here's the...
0
by: Søren Lund | last post by:
Hello, I have implemented a custom config section handler by implementing the IConfigurationSectionHandler interface. I have registered this handler in web.config and everything works fine ......
1
by: Elie Medeiros via .NET 247 | last post by:
Hi there, I have written a custom validator function to validate a datefrom a user-filled field. The function tries to parse the dateand if it can't, sets (serverValidateEventArgs)e.IsValid...
2
by: Robert | last post by:
Hi all I have been trying to set up a framework with logging implemented using the built in Python logging module. For better or worse, I have decided to base the logger names on the module...
2
by: Steve Greenland | last post by:
Apparently I don't understand logging levels. The code: import logging, logging.handlers logging.basicConfig(level=logging.WARNING) newlog = logging.handlers.TimedRotatingFileHandler(...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.