performance tests map/vector
This commit is contained in:
parent
b30a15b746
commit
11c32bb5f1
3 changed files with 182 additions and 44 deletions
|
|
@ -37,6 +37,7 @@ Caner Candan <caner.candan@thalesgroup.com>
|
||||||
#include <cstdio> // used to define EOF
|
#include <cstdio> // used to define EOF
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <algorithm> // std::find
|
||||||
|
|
||||||
#include "eoLogger.h"
|
#include "eoLogger.h"
|
||||||
|
|
||||||
|
|
@ -45,6 +46,13 @@ add a possibility to redirect to several streams
|
||||||
add_redirect/remove_redirect
|
add_redirect/remove_redirect
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef USE_SET
|
||||||
|
typedef std::set<std::ostream*>::iterator StreamIter;
|
||||||
|
#else
|
||||||
|
typedef std::vector<std::ostream*>::iterator StreamIter;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void eoLogger::_init()
|
void eoLogger::_init()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -77,10 +85,10 @@ eoLogger::eoLogger() :
|
||||||
|
|
||||||
eoLogger::~eoLogger()
|
eoLogger::~eoLogger()
|
||||||
{
|
{
|
||||||
//redirect(NULL);
|
//redirect(NULL);
|
||||||
if (_obuf._ownedFileStream != NULL) {
|
if (_obuf._ownedFileStream != NULL) {
|
||||||
delete _obuf._ownedFileStream;
|
delete _obuf._ownedFileStream;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void eoLogger::_createParameters( eoParser& parser )
|
void eoLogger::_createParameters( eoParser& parser )
|
||||||
|
|
@ -161,29 +169,97 @@ eoLogger& operator<<(eoLogger& l, eo::setlevel v)
|
||||||
|
|
||||||
eoLogger& operator<<(eoLogger& l, std::ostream& os)
|
eoLogger& operator<<(eoLogger& l, std::ostream& os)
|
||||||
{
|
{
|
||||||
l._obuf._outStream = &os;
|
#warning deprecated
|
||||||
|
l.addRedirect(os);
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
void eoLogger::redirect(std::ostream& os)
|
void eoLogger::redirect(std::ostream& os)
|
||||||
{
|
{
|
||||||
if (_obuf._ownedFileStream != NULL) {
|
doRedirect(&os);
|
||||||
delete _obuf._ownedFileStream;
|
}
|
||||||
_obuf._ownedFileStream = NULL;
|
|
||||||
}
|
void eoLogger::doRedirect(std::ostream* os)
|
||||||
_obuf._outStream = &os;
|
{
|
||||||
|
if (_obuf._ownedFileStream != NULL) {
|
||||||
|
delete _obuf._ownedFileStream;
|
||||||
|
_obuf._ownedFileStream = NULL;
|
||||||
|
}
|
||||||
|
_obuf._outStreams.clear();
|
||||||
|
if (os != NULL)
|
||||||
|
//_obuf._outStreams.push_back(os);
|
||||||
|
#ifdef USE_SET
|
||||||
|
_obuf._outStreams.insert(os);
|
||||||
|
#else
|
||||||
|
_obuf._outStreams.push_back(os);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void eoLogger::addRedirect(std::ostream& os)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
if (tryRemoveRedirect(&os))
|
||||||
|
//throw eoWrongParamTypeException("Cannot redirect the logger to a stream it is already redirected to");
|
||||||
|
//eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to.";
|
||||||
|
{ eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to."; std::cout << "OK!!!" << std::endl; }*/
|
||||||
|
bool already_there = tryRemoveRedirect(&os);
|
||||||
|
//_obuf._outStreams.push_back(&os);
|
||||||
|
#ifdef USE_SET
|
||||||
|
_obuf._outStreams.insert(&os);
|
||||||
|
#else
|
||||||
|
_obuf._outStreams.push_back(&os);
|
||||||
|
#endif
|
||||||
|
if (already_there)
|
||||||
|
eo::log << eo::warnings << "Cannot redirect the logger to a stream it is already redirected to." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void eoLogger::removeRedirect(std::ostream& os)
|
||||||
|
{
|
||||||
|
if (!tryRemoveRedirect(&os))
|
||||||
|
//throw eoWrongParamTypeException("Cannot remove from the logger a stream it was not redirected to");
|
||||||
|
//throw eoWrongParamTypeException(std::string("Cannot remove from the logger a stream it was not redirected to"));
|
||||||
|
eo::log << eo::warnings << "Cannot remove from the logger a stream it was not redirected to.";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool eoLogger::tryRemoveRedirect(std::ostream* os)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
std::vector<std::ostream*>::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os);
|
||||||
|
//std::cout << _obuf._outStreams.size() << " " << (void*)&*_obuf._outStreams.begin() << " " << (void*)&*_obuf._outStreams.end() << std::endl;
|
||||||
|
//std::cout << "it: " << (void*)&*it << " " << (void*)os << std::endl;
|
||||||
|
if (it == _obuf._outStreams.end())
|
||||||
|
return false;
|
||||||
|
_obuf._outStreams.erase(it);
|
||||||
|
return true;*/
|
||||||
|
/*
|
||||||
|
#ifdef USE_SET
|
||||||
|
std::set<std::ostream*>::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os);
|
||||||
|
if (it == _obuf._outStreams.end())
|
||||||
|
return false;
|
||||||
|
_obuf._outStreams.erase(it);
|
||||||
|
#else
|
||||||
|
std::vector<std::ostream*>::iterator it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os);
|
||||||
|
if (it == _obuf._outStreams.end())
|
||||||
|
return false;
|
||||||
|
_obuf._outStreams.erase(it);
|
||||||
|
#endif*/
|
||||||
|
StreamIter it = find(_obuf._outStreams.begin(), _obuf._outStreams.end(), os);
|
||||||
|
if (it == _obuf._outStreams.end())
|
||||||
|
return false;
|
||||||
|
_obuf._outStreams.erase(it);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void eoLogger::redirect(const char * filename)
|
void eoLogger::redirect(const char * filename)
|
||||||
{
|
{
|
||||||
std::ofstream * os;
|
std::ofstream * os;
|
||||||
if (filename == NULL) {
|
if (filename == NULL) {
|
||||||
os = NULL;
|
os = NULL;
|
||||||
} else {
|
} else {
|
||||||
os = new std::ofstream(filename);
|
os = new std::ofstream(filename);
|
||||||
}
|
}
|
||||||
redirect(*os);
|
doRedirect(os);
|
||||||
_obuf._ownedFileStream = os;
|
_obuf._ownedFileStream = os;
|
||||||
}
|
}
|
||||||
|
|
||||||
void eoLogger::redirect(const std::string& filename)
|
void eoLogger::redirect(const std::string& filename)
|
||||||
|
|
@ -194,18 +270,29 @@ void eoLogger::redirect(const std::string& filename)
|
||||||
|
|
||||||
eoLogger::outbuf::outbuf(const eo::Levels& contexlvl,
|
eoLogger::outbuf::outbuf(const eo::Levels& contexlvl,
|
||||||
const eo::Levels& selectedlvl)
|
const eo::Levels& selectedlvl)
|
||||||
: _outStream(&std::cout), _ownedFileStream(NULL), _contextLevel(contexlvl), _selectedLevel(selectedlvl)
|
:
|
||||||
{}
|
#ifndef USE_SET
|
||||||
|
_outStreams(1, &std::cout),
|
||||||
|
#endif
|
||||||
|
_ownedFileStream(NULL), _contextLevel(contexlvl), _selectedLevel(selectedlvl)
|
||||||
|
{
|
||||||
|
#ifdef USE_SET
|
||||||
|
_outStreams.insert(&std::cout);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
int eoLogger::outbuf::overflow(int_type c)
|
int eoLogger::outbuf::overflow(int_type c)
|
||||||
{
|
{
|
||||||
if (_selectedLevel >= _contextLevel)
|
if (_selectedLevel >= _contextLevel)
|
||||||
{
|
{
|
||||||
if (_outStream && c != EOF)
|
for (StreamIter it = _outStreams.begin(); it != _outStreams.end(); it++)
|
||||||
{
|
{
|
||||||
(*_outStream) << (char) c;
|
if (c != EOF)
|
||||||
}
|
{
|
||||||
}
|
(**it) << (char) c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
(c) Thales group, 2010
|
(c) Thales group, 2010
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -47,6 +48,14 @@ Caner Candan <caner.candan@thalesgroup.com>
|
||||||
#include "eoObject.h"
|
#include "eoObject.h"
|
||||||
#include "eoParser.h"
|
#include "eoParser.h"
|
||||||
|
|
||||||
|
#define USE_SET
|
||||||
|
#undef USE_SET
|
||||||
|
|
||||||
|
#ifdef USE_SET
|
||||||
|
#include <set>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace eo
|
namespace eo
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -133,6 +142,12 @@ private:
|
||||||
//! used by the set of ctors to initiate some useful variables
|
//! used by the set of ctors to initiate some useful variables
|
||||||
void _init();
|
void _init();
|
||||||
|
|
||||||
|
//! helper function regrouping redirection operations; takes a pointer because can be given NULL
|
||||||
|
void doRedirect(std::ostream*);
|
||||||
|
|
||||||
|
//! helper function searching for a stream and removing it, returning true if successful, false otherwise
|
||||||
|
bool tryRemoveRedirect(std::ostream*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* outbuf
|
* outbuf
|
||||||
|
|
@ -142,7 +157,14 @@ private:
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl);
|
outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl);
|
||||||
std::ostream * _outStream;
|
//std::ostream * _outStream;
|
||||||
|
|
||||||
|
#ifdef USE_SET
|
||||||
|
std::set<std::ostream*> _outStreams;
|
||||||
|
#else
|
||||||
|
std::vector<std::ostream*> _outStreams;
|
||||||
|
#endif
|
||||||
|
|
||||||
std::ofstream * _ownedFileStream;
|
std::ofstream * _ownedFileStream;
|
||||||
protected:
|
protected:
|
||||||
virtual int overflow(int_type c);
|
virtual int overflow(int_type c);
|
||||||
|
|
@ -171,12 +193,8 @@ public:
|
||||||
friend eoLogger& operator<<(eoLogger&, eo::setlevel);
|
friend eoLogger& operator<<(eoLogger&, eo::setlevel);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DEPRECATED: Use instead the redirectTo method
|
* DEPRECATED: Use instead the redirect or addRedirect method; has the same effect as addRedirect
|
||||||
* operator<< used there to be able to use std::cout to say that we wish to redirect back the buffer to a standard output.
|
|
||||||
*/
|
*/
|
||||||
//! in order to use stream style to go back to a standart output defined by STL
|
|
||||||
//! and to get retro-compatibility
|
|
||||||
#warning deprecated
|
|
||||||
friend eoLogger& operator<<(eoLogger&, std::ostream&);
|
friend eoLogger& operator<<(eoLogger&, std::ostream&);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,19 @@
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static void test();
|
||||||
|
|
||||||
|
static void test2()
|
||||||
|
{
|
||||||
|
#define NB 100
|
||||||
|
std::ostream* os = (std::ostream*) 1;
|
||||||
|
eo::log.redirect(*os);
|
||||||
|
for (int i = 0; i < NB; i++)
|
||||||
|
eo::log.addRedirect(*(++os));
|
||||||
|
for (int i = 0; i < NB; i++)
|
||||||
|
eo::log.removeRedirect(*(os--));
|
||||||
|
}
|
||||||
|
|
||||||
int main(int ac, char** av)
|
int main(int ac, char** av)
|
||||||
{
|
{
|
||||||
eoParser parser(ac, av);
|
eoParser parser(ac, av);
|
||||||
|
|
@ -26,6 +39,15 @@ int main(int ac, char** av)
|
||||||
make_help(parser);
|
make_help(parser);
|
||||||
make_verbose(parser);
|
make_verbose(parser);
|
||||||
|
|
||||||
|
for (int i = 0; i < 10000; i++)
|
||||||
|
//test();
|
||||||
|
test2();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test()
|
||||||
|
{
|
||||||
eo::log << eo::setlevel(eo::debug);
|
eo::log << eo::setlevel(eo::debug);
|
||||||
|
|
||||||
eo::log << eo::warnings;
|
eo::log << eo::warnings;
|
||||||
|
|
@ -33,40 +55,52 @@ int main(int ac, char** av)
|
||||||
eo::log << "We are writing on the default output stream" << std::endl;
|
eo::log << "We are writing on the default output stream" << std::endl;
|
||||||
|
|
||||||
{
|
{
|
||||||
std::ofstream ofs("logtest.txt");
|
/*std::ofstream ofs("logtest.txt");
|
||||||
eo::log.redirect(ofs);
|
eo::log.redirect(ofs);
|
||||||
eo::log << "In FILE" << std::endl;
|
eo::log << "In FILE" << std::endl;
|
||||||
eo::log.redirect("logtest2.txt");
|
eo::log.redirect("logtest2.txt");
|
||||||
|
eo::log << "In FILE 2" << std::endl;*/
|
||||||
|
eo::log.redirect("logtest.txt");
|
||||||
|
eo::log << "In FILE" << std::endl;
|
||||||
|
std::ofstream ofs("logtest2.txt");
|
||||||
|
eo::log.addRedirect(ofs);
|
||||||
eo::log << "In FILE 2" << std::endl;
|
eo::log << "In FILE 2" << std::endl;
|
||||||
|
eo::log.removeRedirect(ofs);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream ifs("logtest.txt");
|
std::ifstream ifs("logtest2.txt");
|
||||||
//ifs >> str;
|
//ifs >> str;
|
||||||
std::string line;
|
std::string line;
|
||||||
assert(std::getline(ifs, line));
|
assert(std::getline(ifs, line));
|
||||||
assert(line == "In FILE");
|
//std::cout << line << std::endl;
|
||||||
|
assert(line == "In FILE 2");
|
||||||
//std::cout << (line == "In FILE") << std::endl;
|
//std::cout << (line == "In FILE") << std::endl;
|
||||||
assert(!std::getline(ifs, line));
|
assert(!std::getline(ifs, line));
|
||||||
|
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
eo::log.redirect(oss);
|
eo::log.addRedirect(oss);
|
||||||
//eo::log << oss << "In STRINGSTREAM";
|
//eo::log << oss << "In STRINGSTREAM";
|
||||||
eo::log << "In STRINGSTREAM";
|
eo::log << "In STRINGSTREAM";
|
||||||
|
|
||||||
std::cout << "Content of ostringstream: " << oss.str() << std::endl;
|
std::cout << "Content of ostringstream: " << oss.str() << std::endl;
|
||||||
assert(oss.str() == "In STRINGSTREAM");
|
assert(oss.str() == "In STRINGSTREAM");
|
||||||
|
|
||||||
|
eo::log.redirect(std::cout);
|
||||||
|
eo::log << "on COUT" << std::endl;
|
||||||
|
|
||||||
|
|
||||||
ifs.close();
|
ifs.close();
|
||||||
ifs.open("logtest2.txt");
|
ifs.open("logtest.txt");
|
||||||
|
assert(std::getline(ifs, line));
|
||||||
|
assert(line == "In FILE");
|
||||||
assert(std::getline(ifs, line));
|
assert(std::getline(ifs, line));
|
||||||
assert(line == "In FILE 2");
|
assert(line == "In FILE 2");
|
||||||
|
assert(std::getline(ifs, line));
|
||||||
|
assert(line == "In STRINGSTREAM");
|
||||||
assert(!std::getline(ifs, line));
|
assert(!std::getline(ifs, line));
|
||||||
//assert(false);
|
//assert(false);
|
||||||
|
|
||||||
|
|
||||||
eo::log.redirect(std::cout);
|
|
||||||
eo::log << "on COUT" << std::endl;
|
|
||||||
|
|
||||||
eo::log << eo::setlevel("errors");
|
eo::log << eo::setlevel("errors");
|
||||||
eo::log << eo::setlevel(eo::errors);
|
eo::log << eo::setlevel(eo::errors);
|
||||||
|
|
||||||
|
|
@ -83,7 +117,6 @@ int main(int ac, char** av)
|
||||||
eo::log << eo::debug << 4 << ')'
|
eo::log << eo::debug << 4 << ')'
|
||||||
<< "4) in debug mode\n";
|
<< "4) in debug mode\n";
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue