简体   繁体   中英

Redirecting console output/error of third party module to a log file

in my application in c++, i am calling some other third party APIs which generates console output/error messages. i want to redirect all such messages to a log from my application so that user's screen just shows message of my application and is not filled with unrelated message from third party module. Is there a way to do so. I am not sure but third party APIs is using c/c+_ libraries

Thanks Ruchi

If you yourself don't use the standard output file descriptor, you can do this:

  int newout = dup(1);
  int log = open("./my.log", O_RDWR|O_CREAT, 777);
  dup2 (log, 1);

Now the standard output (descriptor 1) goes to "./my.log", and the newout file descriptor goes to what used to be the standard output.

If you use Unix low-level IO (eg write(2) ) you can use newout directly. If you use C stdio, fdopen(3) it. If you prefer C++ streams, look at this question and its answers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM