1

I have been using the following link: https://www.embedded.com/design/programming-languages-and-tools/4215552/Seventeen-steps-to-safer-C-code to enhance the current error logging of a project I am working on.

My end goal is to generate an enum "error type". I would map these error types to a lookup table of char*'s which I could use to log a more detailed explanation of what the error is.

This way, I have all my error codes and corresponding error strings in a central location and can easily modify/lookup a message and errorcode without having to dig through the code.

As a PoC, my header file contains the following:

typedef enum {
    ECODE_OK = 0,         // OK
    ECODE_SAMPLE_ERR = 1,
    ECODE_LAST
} myEcodes;

char * ecodeMap[ECODE_LAST+1];
char * ecodeName(myEcodes err);

My implementation in C of the header is included below:

char * ecodeMap[ECODE_LAST+1] =
{
    "No error to report",
    "Example Error Code - Invalid Value",
    "Last ecode place holder"
};

char * ecodeName(myEcodes err)
{
    return (char *) eyescanEcodeMap[err];
}

My question is, say I have the following code snippet to log an error I encountered:

fprintf(fp, "%s", ecodeName(ECODE_SAMPLE_ERR));

What if I want ECODE_SAMPLE_ERR to actually contain a formatted string like

"Example Error Code - Invalid Values: %d %d", myVarInt1, myVarInt2

instead of just

"Example Error Code - Invalid Value"

What would be the best way to be able to format individual strings in my array of char* such that I can include the value of variables in particular entries?

7
  • how many formatted strings do you expected to use? that is - how many different types of the formatted strings will you have ? Commented Apr 25, 2018 at 1:46
  • It depends on the error. Some may just be strings with no formatting, i.e. "You have lost connection to the device". Others may have several variables, i.e. "The current laser settings for DAC %d, pulsewidth %d, and period %d resulted in an invalid laser power %f", var1, var2, var3, var4. I would say most won't need errors and only a very small number would ever need more than 5 variables inserted into the string Commented Apr 25, 2018 at 1:51
  • I don't understand something. Is the actual log function calling fprintf(fp, "%s", get_error_string(error_code));. Forgive me for changing the names, I tried to type it like you did but I couldn't. Commented Apr 25, 2018 at 1:57
  • 1
    I think these 2 links can help you out : stackoverflow.com/questions/11180695/… stackoverflow.com/questions/28622895/… Commented Apr 25, 2018 at 2:20
  • I hope you do know that the statement: strerror( error ); will return a suitable string for most error conditions. However, for errors specific to the application, something like this: In the code: CommonWriteCdsLog( eLL_Critical, get_pFormatString( eFormat_CFFL_string ), __FILE__, __LINE__, "LibFunc:popen() failed for 'bin/date'"); worked very well for me. were ` get_pFormatString()` selected from a table of format strings and eLL_Critical was from a enum of values to determine if the message was to be logged. and the (cont) Commented Apr 25, 2018 at 18:49

1 Answer 1

1

If the main aim is to add print along with other variable values, let ERROR string be constant. You can just add additional values when you are writing to file fp.

fprintf(fp, "%s: %d %d", ecodeName(ECODE_SAMPLE_ERR), myVarInt1, myVarInt2);
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, there will be times I need to actually insert the variable values into different parts of the string, not just append them onto the end
It's best to keep the error codes simple. These kind of ERROR codes are meant to be constant. I don't understand why you have to append in between these strings. May be you can change the strings to be as simple as possible and generic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.