Redirecting standard output to syslog

前端 未结 5 1279
半阙折子戏
半阙折子戏 2021-01-01 20:57

I\'m planning to package OpenTibia Server for Debian. One of the things I want to do is add startup via /etc/init.d and daemonization of the otserv

5条回答
  •  离开以前
    2021-01-01 21:13

    You can redirect any stream in C++ via the rdbuf() command. This is a bit convoluted to implement but not that hard.

    You need to write a streambuf that would output to syslog on overflow(), and replace the std::cout rdbuf with your streambuf.

    An example, that would output to a file (no error handling, untested code)

    #include 
    #include 
    using namespace std;
    
    int main (int argc, char** argv) {
       streambuf * yourStreamBuffer = NULL;
       ofstream outputFileStream;
       outputFileStream.open ("theOutputFile.txt");
    
       yourStreamBuffer = outputFileStream.rdbuf();
       cout.rdbuf(yourStreamBuffer);
    
       cout << "Ends up in the file, not std::cout!";
    
       outputFileStream.close();
    
       return 0;
     }
    

提交回复
热议问题