title: How to generate a log file names with sequence numbers?
body: <p>I want to dump the logs into a log file. After the max size limit of the file is reached I want to rename the file with a number ( format will be like log.%d).</p>
  <p>There is a limit to the number of backup log files.
  after the limit is reached I have to delete the oldest created log file too.</p>
  <pre><code>
  void log_msg( )
  {
      char log_message[256];
      time_t curr_time;
      struct tm * time_info;
      int ret;
      char NewLogFileName[40];
      char oldLogFileName[40];
      struct stat sb;
      int logNum = 0;
  
      time( &amp;curr_time );
      time_info = localtime(&amp;curr_time);
      strftime(log_message, sizeof(log_message), &quot;%d %b %Y %H:%M:%S,&quot;, time_info);
  
  
       if ( files_in_Dir() &gt; LOG_MAX_NUM_FILES  ) {
      /* delete the last created file and rename other files accordingly
        ex: file.0 -&gt; file.1, file.1 -&gt; file.2 */
       sprintf(oldLogFileName, &quot;%s/%s.%d&quot;, LOG_DIR, logBaseFileName, (LOG_MAX_NUM_FILES - 1));
       unlink (oldLogFileName);
  
       for(logNum = 0; logNum &lt; LOG_MAX_NUM_FILES - 1; logNum++ ) {
        sprintf(oldLogFileName, &quot;%s/%s.%d&quot;, LOG_DIR ,logBaseFileName ,logNum);
        sprintf(NewLogFileName, &quot;%s/%s.%d&quot;, LOG_DIR ,logBaseFileName ,logNum+1);
        if (rename(oldLogFileName,NewLogFileName) == 0) {
            printf(&quot;%s: Moved the content of %s to %s \n&quot;, __FUNCTION__, oldLogFileName, NewLogFileName);
        }else{
            printf(&quot;%s: Can't move the content of %s to %s %s \n&quot;, __FUNCTION__, oldLogFileName, NewLogFileName, strerror(errno));
        }
      }
    }
  
   // check for the file size before every write
      if ((stat(logFileName, &amp;sb) == 0) &amp;&amp; (sb.st_size &gt; LOG_FILE_SIZE)) {
       // close the file
        close(data_log_fd);
           data_log_fd = 0;
         for(logNum = 0; logNum &lt; LOG_MAX_NUM_FILES - 1; logNum++ ) {
        sprintf(oldLogFileName, &quot;%s/%s.%d&quot;, LOG_DIR ,logBaseFileName ,logNum);
        sprintf(NewLogFileName, &quot;%s/%s.%d&quot;, LOG_DIR ,logBaseFileName ,logNum+1);
        if (rename(oldLogFileName,NewLogFileName) == 0) {
            printf(&quot;%s: Moved the content of %s to %s \n&quot;, __FUNCTION__, oldLogFileName, NewLogFileName);
        }else{
            printf(&quot;%s: Can't move the content of %s to %s %s \n&quot;, __FUNCTION__, oldLogFileName, NewLogFileName, strerror(errno));
        }
      }
      
       // reinitiate logging
        data_log_init();
    }
  
      strcat(log_message, cpu_data);
  
      /** Finally write the log message to the log file */
     if( data_log_fd &gt; 0 )
          ret = write( data_log_fd, (const void*) log_message, strlen(log_message) );
      else
          printf(&quot;Could not write data to the log file: %s\n&quot;, strerror(errno) );
  
    return;
  
  }
  </code></pre>
  <p>There was some error in renaming, it is renaming the file log.0 to log.4 directly. No intermediate files are getting generated.</p>