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 8657957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:38:58+00:00 2026-06-12T15:38:58+00:00

I am not sure exactly how to pass different string types to functions in

  • 0

I am not sure exactly how to pass different string types to functions in C.

I have a function that receives a formatted string

void Logger::info(const char *logstring, ...) 
{

    // So I basically can write a preliminary message "DATA:" to a buffer
    // and then use vsnprintf() to write the const char *logstring to the buffer as well.

    int offset = 0;
    va_list argp;
    char buf[STRSIZE + 1];

    va_start(argp, logstring);
    offset = sprintf(buf, "DATA: ");
    vsnprintf(&buf[offset], STRSIZE - offset, logstring, argp);
    va_end(argp);

    // Then I write the buffer to stderr

    fprintf(stderr, "%s\n", buf);
}

What I would like to do is move all of that deeper into the function. But I cannot make it work because I have no idea how to successfully pass the formatted string “logstring” to the function and have it come out the other side.

So I want to do it like this…

info(const char *logstring, ...)
{
    writeToFile("Data:",logstring);
}

Then I can have the log file writing done more centrally.

  • 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-06-12T15:39:00+00:00Added an answer on June 12, 2026 at 3:39 pm

    Ok, its higly likely I didn’t get exaclty where you’re looking for here, but willing to take a shot and burn an answer. The following is about as basic a logger as you can get, but demonstrates how to centralize the message-build. Included at the bottom are some test conditions, and I purposely undersized the initial buffer size to allow you walk through in a debugger and see the resizes interactively. You will no-doubt want to size that up.

    Anyway, if I happened to get luck and understand the question correctly, I hope if it doesn’t directly address your issue, you at least get some ideas. Thanks.

    EDIT: OP requested to squelch using std::string and buffer resizing to allow conformance with the limited compilation capabilities of his embedded system environment. Fixed length buffers are used, therefore, and output from vsnprintf() is only trusted if content actually fit.

    #include <stdio.h>
    #include <stdarg.h>
    #include <string.h>
    
    class Logger
    {
        static const size_t STRSIZE = 256;
    
    public:
        void error(const char* fmt, ...)
        {
            va_list argptr;
            va_start(argptr,fmt);
            log("ERROR", fmt, argptr);
        };
    
        // two different logging interfaces.
        void info(const char* fmt, ...)
        {
            va_list argptr;
            va_start(argptr,fmt);
            log("INFO", fmt, argptr);
        };
    
        void debug(const char* fmt, ...)
        {
            va_list argptr;
            va_start(argptr,fmt);
            log("DEBUG", fmt, argptr);
        };
    
    private:
        void log(const char *msg, const char* fmt, va_list& vl)
        {
            // uses a fixed size message buffer
            char str[STRSIZE+1] = {0};
            strncpy(str, msg, sizeof(str)-1);
            strncat(str, ": ", (sizeof(str)-1) - strlen(str));
    
            // needed for sizing limits of variadic printf, then send
            //  output as a single line message to stderr.
            size_t mlen = strlen(str);
            if (vsnprintf(str + mlen, sizeof(str)-mlen-1, fmt, vl) >= 0)
                fprintf(stderr, "%s\n", str);
            else
                fprintf(stderr, "%s: (max log message length exceeded)\n", msg);
        };
    };
    
    int main(int argc, char *argv[])
    {
        Logger logger;
    
        logger.debug("Numbers %d %d %d", 1,2,3);
        logger.error("Strings %s %s %s", "1", "2", "3");
        logger.info("Mixed %s %d %p", "1", 2, "3");
        logger.info("No additional parameters required for this message.");
    
        // demonstrate automatic cutoff.
        char sbig[] = "0123456789012345678901234567890123456789"
                      "0123456789012345678901234567890123456789";
    
        logger.debug("Oversized params: %d-%s %d-%s %d-%s %d-%s", 
                     1, sbig, 2, sbig, 3, sbig, 4, sbig);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass a variable via jquery ajax call. I'm not exactly sure
The problem i'm having is with the string parameter. I'm not exactly sure how
Coming from a desktop background I'm not sure exactly how to pass the exceptions
(I'm not sure exactly how to phrase the title here, and because of that
Not sure exactly what I need to do to make this work, so my
I'm not sure exactly how to describe what I want to do, so I'll
I'm not sure exactly how to explain this, so I'll just start with an
I'm not sure exactly how to describe what I want. I want to define
I'm not sure exactly where this is happening, but what I'm trying to do
I'm not sure exactly how to understand the usage of message in socket.io. I'm

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.