4

I'm trying to print some text in common log format.

printf("%s - - [%s] %s %d %zu\n", ip, _time, row, statuscode, size);

The problem is that the order gets all mixed up. The output is:

200 1511 - - [20/Sep/2017:13:07:32 +0200] GET / HTTP/1.1

I think (1511) is the ip getting printed. Don't know why.

When I print them like this:

printf("1. %s\n", ip);
printf("2. %s\n", _time);
printf("3. %s\n", row);
printf("4. %d\n", statuscode);
printf("5. %zu\n", size);

It works like expected:

1. 127.0.0.1
2. 20/Sep/2017:13:11:24 +0200
3. GET / HTTP/1.1
4. 200
5. 151

It seems the problem starts when i add statuscode for some reason. I have no clue why. Any help is appreciated.

Here is the function where I use prinft():

static void handlelogging(char* method, struct sockaddr_storage client_addr, size_t size, char* row, int statuscode) {

char* ip;
char _time[80];
struct tm *info;
time_t rawtime;

time(&rawtime);
info = localtime(&rawtime);
strftime(_time, 80,"%d/%b/%Y:%H:%M:%S %z", info)
ip = getip(client_addr);

//Print goes here, see above.
}

As someone said it could be a problem with the ip variable:

static char* getip(struct sockaddr_storage client_addr) {
    char ipstr[20];
    struct sockaddr_in *s;


    s = (struct sockaddr_in *) &client_addr;

    /**
     * Converts network address (s) in the IPV_4 family into a string.
     */
    return strdup(inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr));
 }
5
  • 1
    Are you sure that the data types match? Otherwise you have undefined behaviour. Sep 20, 2017 at 11:22
  • 7
    Please post MCVE
    – vasek
    Sep 20, 2017 at 11:23
  • Questions seeking debugging help (why isn't this code working?) must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Sep 20, 2017 at 11:27
  • In your example "ip" variable points to some other place, where the string "200 151..." is stored, which is surprisingly similar to not-fully-parsed-yet status code and length. Double-check where it is pointing and its null-termination.
    – Lyth
    Sep 20, 2017 at 11:27
  • 1
    The od utility (originally "octal dump", IIRC) is a useful tool here, if you're running on a POSIX or POSIX-like system. Instead of myprogram, run something like myprogram | od -c to see every character printed, including control characters like a \r carriage return. Sep 20, 2017 at 12:19

1 Answer 1

7

I think that the problem comes from row which contains a \r character.

Just modify it with strchr(...)

/* replace all '\r' by 'R' in row */
char *p = strchr(row, '\r');
while(p)
{ 
    *p = 'R';
    p = strchr(row, '\r');
}
2
  • When I use that the program doesn't print anything. Do I need to do anything else?
    – gel
    Sep 20, 2017 at 11:37
  • I think you are indeed correct, however I would change '\r' for '\n' I think that was the intended use?
    – Girauder
    Sep 20, 2017 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.