Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7022749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:37:19+00:00 2026-05-27T23:37:19+00:00

I want to know why is it preferred to redirect the debug statements to

  • 0

I want to know why is it preferred to redirect the debug statements to stderr like it is done here:

#ifdef DEBUG_TEST
  #define DEBUG_TEST 1
  #else
  #define DEBUG_TEST 0
  #endif

  #define DEBUG_PRINT(fmt, ...) \
             do { if (DEBUG_TEST) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0)

Also: How can we redirect these debug statements to a separate log file with a timestamp on that file? I want to do this using a macro in my code.

Platform: Linux, gcc compiler

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:37:20+00:00Added an answer on May 27, 2026 at 11:37 pm

    I can give you two examples of why you would do this: You could then easily redirect standard output to a file while keeping standard error in your terminal (and this seems to be exactly what you want to do in your second question). Your tool(s) might highlight messages sent to stderr making them easier to spot right away.

    You could use the command line version to redirect stderr, as thesamet suggests. To get a timestamp in the filename, you could do something like this when you run your program:

    ./program 2>"logfile-`date`.txt"
    

    If you want to do it in the program itself, one way is to simply use fopen to open another file and write to that. Here is a fully working example you can play with:

    #include <time.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <stdbool.h>
    
    #define DEBUG_TEST true
    
    FILE *debug_file;
    
    #define DEBUG_PRINT(fmt, ...) \
            do { if (DEBUG_TEST) fprintf(debug_file, fmt, ##__VA_ARGS__); } while (false);
    
    
    
    int main()
    {
            time_t timestamp = time(NULL);
            char * s = malloc(snprintf(NULL, 0, "debug-%d.txt", timestamp));
            sprintf(s, "debug-%d.txt", timestamp);
    
            debug_file=fopen(s, "w");
    
            DEBUG_PRINT("YEAH\n");
    
            fclose(debug_file);
    
            return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Want to know which one is preferred while coding to use Static Methods
want to know why String behaves like value type while using ==. String s1
I want to know the difference between : $this->forward(module, action); And $this->redirect(module/action); My first
I want to know which is the preferred way to declare a string in
I want to know why Annotations are preferred over XML in Struts2 Applications?
I'm pretty new to Scala but I like to know what is the preferred
i want know if is possible, to get a specific element value of a
I'm making a chat responder for a game and i want know if there
Want to know what is the difference between ipv4 and ipv6 and how does
Want to know ways to store OBJECTS of a Class in some persistent storage.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.