Browse Source

set log file

pull/5/head
orignal 7 years ago
parent
commit
a368f44de4
  1. 54
      i2psam/i2psam.cpp
  2. 16
      i2psam/i2psam.h
  3. 2
      i2psam/i2psam.pro
  4. 2
      i2psam/makefile.unix

54
i2psam/i2psam.cpp

@ -10,6 +10,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <stdarg.h> #include <stdarg.h>
#include <fstream>
#ifndef WIN32 #ifndef WIN32
#include <errno.h> #include <errno.h>
@ -29,9 +30,9 @@ namespace SAM
static void print_error(const std::string& err) static void print_error(const std::string& err)
{ {
#ifdef WIN32 #ifdef WIN32
std::cout << err << "(" << WSAGetLastError() << ")" << std::endl; StreamSession::getLogStream () << err << "(" << WSAGetLastError() << ")" << std::endl;
#else #else
std::cout << err << "(" << errno << ")" << std::endl; StreamSession::getLogStream () << err << "(" << errno << ")" << std::endl;
#endif #endif
} }
@ -149,7 +150,7 @@ void Socket::write(const std::string& msg)
print_error("Failed to send data because socket is closed"); print_error("Failed to send data because socket is closed");
return; return;
} }
std::cout << "Send: " << msg << std::endl; StreamSession::getLogStream () << "Send: " << msg << std::endl;
ssize_t sentBytes = send(socket_, msg.c_str(), msg.length(), 0); ssize_t sentBytes = send(socket_, msg.c_str(), msg.length(), 0);
if (sentBytes == SAM_SOCKET_ERROR) if (sentBytes == SAM_SOCKET_ERROR)
{ {
@ -186,7 +187,7 @@ std::string Socket::read()
close(); close();
print_error("Socket was closed"); print_error("Socket was closed");
} }
std::cout << "Reply: " << buffer << std::endl; StreamSession::getLogStream () << "Reply: " << buffer << std::endl;
return std::string(buffer); return std::string(buffer);
} }
@ -250,7 +251,7 @@ StreamSession::StreamSession(
, isSick_(false) , isSick_(false)
{ {
myDestination_ = createStreamSession(destination); myDestination_ = createStreamSession(destination);
std::cout << "Created a brand new SAM session (" << sessionID_ << ")" << std::endl; getLogStream () << "Created a brand new SAM session (" << sessionID_ << ")" << std::endl;
} }
StreamSession::StreamSession(StreamSession& rhs) StreamSession::StreamSession(StreamSession& rhs)
@ -268,13 +269,13 @@ StreamSession::StreamSession(StreamSession& rhs)
for(ForwardedStreamsContainer::const_iterator it = rhs.forwardedStreams_.begin(), end = rhs.forwardedStreams_.end(); it != end; ++it) for(ForwardedStreamsContainer::const_iterator it = rhs.forwardedStreams_.begin(), end = rhs.forwardedStreams_.end(); it != end; ++it)
forward(it->host, it->port, it->silent); forward(it->host, it->port, it->silent);
std::cout << "Created a new SAM session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl; getLogStream () << "Created a new SAM session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl;
} }
StreamSession::~StreamSession() StreamSession::~StreamSession()
{ {
stopForwardingAll(); stopForwardingAll();
std::cout << "Closing SAM session (" << sessionID_ << ") ..." << std::endl; getLogStream () << "Closing SAM session (" << sessionID_ << ") ..." << std::endl;
} }
/*static*/ /*static*/
@ -297,16 +298,16 @@ std::string StreamSession::generateSessionID()
return result; return result;
} }
RequestResult<std::auto_ptr<Socket> > StreamSession::accept(bool silent) RequestResult<std::shared_ptr<Socket> > StreamSession::accept(bool silent)
{ {
typedef RequestResult<std::auto_ptr<Socket> > ResultType; typedef RequestResult<std::shared_ptr<Socket> > ResultType;
std::auto_ptr<Socket> streamSocket(new Socket(socket_)); std::shared_ptr<Socket> streamSocket(new Socket(socket_));
const Message::eStatus status = accept(*streamSocket, sessionID_, silent); const Message::eStatus status = accept(*streamSocket, sessionID_, silent);
switch(status) switch(status)
{ {
case Message::OK: case Message::OK:
return RequestResult<std::auto_ptr<Socket> >(streamSocket); return std::move (RequestResult<std::shared_ptr<Socket> >(streamSocket));
case Message::EMPTY_ANSWER: case Message::EMPTY_ANSWER:
case Message::CLOSED_SOCKET: case Message::CLOSED_SOCKET:
case Message::INVALID_ID: case Message::INVALID_ID:
@ -319,11 +320,11 @@ RequestResult<std::auto_ptr<Socket> > StreamSession::accept(bool silent)
return ResultType(); return ResultType();
} }
RequestResult<std::auto_ptr<Socket> > StreamSession::connect(const std::string& destination, bool silent) RequestResult<std::shared_ptr<Socket> > StreamSession::connect(const std::string& destination, bool silent)
{ {
typedef RequestResult<std::auto_ptr<Socket> > ResultType; typedef RequestResult<std::shared_ptr<Socket> > ResultType;
std::auto_ptr<Socket> streamSocket(new Socket(socket_)); std::shared_ptr<Socket> streamSocket(new Socket(socket_));
const Message::eStatus status = connect(*streamSocket, sessionID_, destination, silent); const Message::eStatus status = connect(*streamSocket, sessionID_, destination, silent);
switch(status) switch(status)
{ {
@ -345,13 +346,13 @@ RequestResult<void> StreamSession::forward(const std::string& host, uint16_t por
{ {
typedef RequestResult<void> ResultType; typedef RequestResult<void> ResultType;
std::auto_ptr<Socket> newSocket(new Socket(socket_)); std::shared_ptr<Socket> newSocket(new Socket(socket_));
const Message::eStatus status = forward(*newSocket, sessionID_, host, port, silent); const Message::eStatus status = forward(*newSocket, sessionID_, host, port, silent);
switch(status) switch(status)
{ {
case Message::OK: case Message::OK:
forwardedStreams_.push_back(ForwardedStream(newSocket.get(), host, port, silent)); forwardedStreams_.push_back(ForwardedStream(newSocket.get(), host, port, silent));
newSocket.release(); // release after successful push_back only newSocket = nullptr; // release after successful push_back only
return ResultType(true); return ResultType(true);
case Message::EMPTY_ANSWER: case Message::EMPTY_ANSWER:
case Message::CLOSED_SOCKET: case Message::CLOSED_SOCKET:
@ -370,7 +371,7 @@ RequestResult<const std::string> StreamSession::namingLookup(const std::string&
typedef RequestResult<const std::string> ResultType; typedef RequestResult<const std::string> ResultType;
typedef Message::Answer<const std::string> AnswerType; typedef Message::Answer<const std::string> AnswerType;
std::auto_ptr<Socket> newSocket(new Socket(socket_)); std::shared_ptr<Socket> newSocket(new Socket(socket_));
const AnswerType answer = namingLookup(*newSocket, name); const AnswerType answer = namingLookup(*newSocket, name);
switch(answer.status) switch(answer.status)
{ {
@ -391,7 +392,7 @@ RequestResult<const FullDestination> StreamSession::destGenerate() const
typedef RequestResult<const FullDestination> ResultType; typedef RequestResult<const FullDestination> ResultType;
typedef Message::Answer<const FullDestination> AnswerType; typedef Message::Answer<const FullDestination> AnswerType;
std::auto_ptr<Socket> newSocket(new Socket(socket_)); std::shared_ptr<Socket> newSocket(new Socket(socket_));
const AnswerType answer = destGenerate(*newSocket); const AnswerType answer = destGenerate(*newSocket);
switch(answer.status) switch(answer.status)
{ {
@ -576,6 +577,23 @@ const std::string& StreamSession::getSAMVersion() const
return socket_.getVersion(); return socket_.getVersion();
} }
std::shared_ptr<std::ostream> StreamSession::logStream;
std::ostream& StreamSession::getLogStream ()
{
return logStream ? *logStream : std::cout;
}
void StreamSession::SetLogFile (const std::string& filename)
{
logStream = std::make_shared<std::ofstream> (filename, std::ofstream::out | std::ofstream::app);
}
void StreamSession::CloseLogFile ()
{
logStream = nullptr;
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

16
i2psam/i2psam.h

@ -11,6 +11,7 @@
#include <stdint.h> #include <stdint.h>
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <ostream>
#ifdef WIN32 #ifdef WIN32
//#define _WIN32_WINNT 0x0501 //#define _WIN32_WINNT 0x0501
@ -278,7 +279,7 @@ struct RequestResult
}; };
template<class T> template<class T>
struct RequestResult<std::auto_ptr<T> > struct RequestResult<std::shared_ptr<T> >
{ {
// a class-helper for resolving a problem with conversion from temporary RequestResult to non-const RequestResult& // a class-helper for resolving a problem with conversion from temporary RequestResult to non-const RequestResult&
struct RequestResultRef struct RequestResultRef
@ -291,12 +292,12 @@ struct RequestResult<std::auto_ptr<T> >
}; };
bool isOk; bool isOk;
std::auto_ptr<T> value; std::shared_ptr<T> value;
RequestResult() RequestResult()
: isOk(false) {} : isOk(false) {}
explicit RequestResult(std::auto_ptr<T>& value) explicit RequestResult(std::shared_ptr<T>& value)
: isOk(true), value(value) {} : isOk(true), value(value) {}
@ -348,8 +349,8 @@ public:
static std::string generateSessionID(); static std::string generateSessionID();
RequestResult<std::auto_ptr<Socket> > accept(bool silent); RequestResult<std::shared_ptr<Socket> > accept(bool silent);
RequestResult<std::auto_ptr<Socket> > connect(const std::string& destination, bool silent); RequestResult<std::shared_ptr<Socket> > connect(const std::string& destination, bool silent);
RequestResult<void> forward(const std::string& host, uint16_t port, bool silent); RequestResult<void> forward(const std::string& host, uint16_t port, bool silent);
RequestResult<const std::string> namingLookup(const std::string& name) const; RequestResult<const std::string> namingLookup(const std::string& name) const;
RequestResult<const FullDestination> destGenerate() const; RequestResult<const FullDestination> destGenerate() const;
@ -371,6 +372,10 @@ public:
bool isSick() const; bool isSick() const;
static std::ostream& getLogStream ();
static void SetLogFile (const std::string& filename);
static void CloseLogFile ();
private: private:
StreamSession(const StreamSession& rhs); StreamSession(const StreamSession& rhs);
StreamSession& operator=(const StreamSession& rhs); StreamSession& operator=(const StreamSession& rhs);
@ -395,6 +400,7 @@ private:
const std::string i2pOptions_; const std::string i2pOptions_;
ForwardedStreamsContainer forwardedStreams_; ForwardedStreamsContainer forwardedStreams_;
mutable bool isSick_; mutable bool isSick_;
static std::shared_ptr<std::ostream> logStream;
void fallSick() const; void fallSick() const;
FullDestination createStreamSession(const std::string &destination); FullDestination createStreamSession(const std::string &destination);

2
i2psam/i2psam.pro

@ -10,7 +10,7 @@ TARGET = i2psam
TEMPLATE = lib TEMPLATE = lib
CONFIG += staticlib CONFIG += staticlib
QMAKE_CXXFLAGS += -Wall QMAKE_CXXFLAGS += -Wall -std=c++11
SOURCES += i2psam.cpp SOURCES += i2psam.cpp

2
i2psam/makefile.unix

@ -11,7 +11,7 @@
CC = gcc CC = gcc
CXX = g++ CXX = g++
CFLAGS = -pipe -O2 -fPIC -Wall -W -D_REENTRANT $(DEFINES) CFLAGS = -pipe -O2 -fPIC -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS = -pipe -O2 -fPIC -Wall -W -D_REENTRANT $(DEFINES) CXXFLAGS = -pipe -O2 -std=c++11 -fPIC -Wall -W -D_REENTRANT $(DEFINES)
AR = ar cqs AR = ar cqs
RANLIB = RANLIB =
TAR = tar -cf TAR = tar -cf

Loading…
Cancel
Save