I am trying to print some text in a general log format.
printf("%s - - [%s] %s %d %zu\n", ip, _time, row, statuscode, size);
The problem is that the order is all mixed up. Output:
200 1511 - - [20/Sep/2017:13:07:32 +0200] GET / HTTP/1.1
I think ( 1511
) is typing ip
. I do not know why.
When I print them as follows:
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 as 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 idea why. Any help is appreciated.
Here is the function in which I use prnft ():
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);
}
As someone said this 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;
return strdup(inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr));
}