c++ - Invoke format log method of a static logger -


i have code in project, can't find why can't got format argument in log file. when invoke logger this:

logger::get_logger().repare().log("log something"); 

but, log file still opened , content "log : " in log() method shown in log file indicate fact code works. : expected :

======== new log ======= repare.log : time : 19:05:17. log 

but :

======== new log ======= repare.log : 

i thought code work around vsprintf right, copied tutorial.

here code.

 class logger : private noncopyable {         public :             logger();             ~logger();             static logger& get_logger();             logger& repare();             int log(const char* format, ...);         private :             string filename_;             int fd_;     };  int my_http::deploylogfile() {     char filename[100];     int fd;     sprintf(filename, "%s/stuff/log.txt", get_my_root_path);     fd = open(filename, o_wronly | o_trunc | o_append);     if (fd == -1) {         fd = open(filename, o_wronly | o_creat | o_trunc | o_append, s_irusr | s_iwusr | s_iroth);     } else {         cout << "log exist!!!" << endl;     }     exit_if(fd<0, "error in open log file");     char buff[100];     sprintf(buff, "======== new log =======\n");     write(fd, buff, strlen(buff));     return fd; }  logger::logger() {     fd_ = -1; }  logger::~logger() {     if (fd_ != -1) {         close(fd_);     } }  logger& logger::get_logger() {     static logger lg;     return lg; }  logger& logger::repare() {     int fd = deploylogfile();     if (fd == -1) {         perror("deploy log fail");     } else {         fd_ = fd;     }     char buff[100];     sprintf(buff, "repare.");     write(fd, buff, strlen(buff));     return *this; }  int logger::log(const char* format, ...) {     va_list arglist;     va_start(arglist, format);      char buf[100];     sprintf(buf, "log : ");     write(fd_, buf, strlen(buf));      char buff[500];     sprintf(buff, "time : %s. ", __time__);     vsprintf(buff, format, arglist);     sprintf(buff, "\n");      write(fd_, buff, strlen(buff));     va_end(arglist);     return strlen(buff); } 

well, it's because overwrote buff "\n", of course contains 0 after new line, strlen checks '0', writes new line buff file. meant was:

#include <string.h> #include <iostream> #include <stdarg.h> #include <fcntl.h> #include <cstdio> #include <cstdlib> #include <unistd.h>  using namespace std;  class logger {         public :             logger();             ~logger();             static logger& get_logger();             logger& repare();             int log(const char* format, ...);         private :             string filename_;             int fd_; };  int deploylogfile() {     char filename[100];     int fd;     sprintf(filename, "%s/log.txt", "/tmp");     fd = open(filename, o_wronly | o_trunc | o_append);     if (fd == -1) {         fd = open(filename, o_wronly | o_creat | o_trunc | o_append, s_irusr | s_iwusr | s_iroth);     } else {         cout << "log exist!!!" << endl;     }     if (fd<0)      {      write(1, "error in open log file", strlen("error in open log file"));     exit(-1);     }     char buff[100];     sprintf(buff, "======== new log =======\n");     write(fd, buff, strlen(buff));     return fd; }  logger::logger() {     fd_ = -1; }  logger::~logger() {     if (fd_ != -1) {         close(fd_);     } }  logger& logger::get_logger() {     static logger lg;     return lg; }  logger& logger::repare() {     int fd = deploylogfile();     if (fd == -1) {         perror("deploy log fail");     } else {         fd_ = fd;     }     char buff[100];     sprintf(buff, "repare.");     write(fd, buff, strlen(buff));     return *this; }  int logger::log(const char* format, ...) {     va_list arglist;     va_start(arglist, format);      char buf[100];     sprintf(buf, "log : ");     write(fd_, buf, strlen(buf));      char buff[500];     sprintf(buff, "time : %s. ", __time__);     int offset = strlen(buff) ;     vsprintf(buff + offset, format, arglist);     sprintf(buff + strlen(buff), "\n");     write(fd_, buff, strlen(buff));     va_end(arglist);     return strlen(buff); }  int main(void){      logger::get_logger().repare().log("%s", "log something");     return 0; } 

outputs

======== new log ======= repare.log : time : 19:05:17. log 

but - it's not wanted - since trying write:

logger::get_logger().repare().log("%s", "log something"); logger::get_logger().repare().log("%s %d %s", "crazy whatamai", 42, "log more"); 

would leave

log exist!!! ======== new log ======= repare.log : time : 19:06:17. crazy whatamai 42 log more 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -