-7

Is it too long for me that not writing any code. I have try to write a simple demo in C, but meet below,

 1 main.c|87 col 21 warning| passing argument 2 of ‘Logger’ makes pointer from integer without a cast [-Wint-conversion]              
2 logger.h|24 col 6 error| note: expected ‘int * (*)()’ but argument is of type ‘int’

I define a function

void Logger(int priority, int errno, const char* fmt, ...);

The 2nd arg of Logger is in type int. However, when I want to use it in my main process,

Logger(LOG_ERR, errno, "ERROR: select failed");

It inform me that the expected type of 2nd arg is ‘int * (*)()’ . In my call of the function, the 2nd actual param, errno, is from errno.h

The code as below 1. logger.c

// logger.c
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <error.h>

    #include "logger.h"

    char LogLastMsg[128]; // all info of the last log, all the info to log last time

    int Log2Stderr = LOG_INFO; //control Loging to stderr


    void init_logger(char* logfile_path, FILE* logfile)
    {
        logfile = fopen(logfile_path, "w+");
        if ( NULL == logfile )
        {
            fprintf(stderr, "Failed to init logger");
            exit(-1);
        }
    }

    void end_logger(FILE *logfile)
    {
        fclose(logfile);
    }

    /**
     * @Synopsis  a log func demo 
     *      demo for how  user defined module log info
     *
     * @Param priority: level of log, LOG_ERR, LOG_DEBUG etc.
     * @Param errno:    errno
     * @Param fmt:  format of message to log
     * @Param ...:  args follow by fmt
     */
    void Logger(int priority, int errno, char* fmt, ...)
    {
        char priVc[][8] = {"Emerg", "Alert", "Crit", "Error", "Warning", "Notice", "Info", "Debug"};

        char* priPt = priority < 0 || priority >= sizeof(priVc)/sizeof(priVc[0]) ?
            "Unknow priority!" : priVc[priority];

        char *errMsg = errno <= 0 ? NULL : strerror(errno);

        {
            va_list argPt;
            unsigned Ln;

            va_start(argPt, fmt);  //now argPt is point to mylog's param:...
            Ln = snprintf(LogLastMsg, sizeof(LogLastMsg), "[mylog...][%s]: ", priPt);
            Ln += vsnprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, fmt, argPt);
            if (NULL != errMsg)
            {
                Ln += snprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, "%d:%s", errno, errMsg);
            }
            va_end(argPt);
        }
        //choose the log which should be show on stderr
        if (priority < LOG_ERR || priority <= Log2Stderr)
        {
            fprintf(stderr, "%s", LogLastMsg);
        }

        //always to syslog
        syslog(priority, "%s", LogLastMsg);

        if (priority <= LOG_ERR)
        {
            exit(-1);
        }
        return ;
    }




/**
 * @file main.c
 * @synopsis  
 * @author Windeal, lipeilun4it@outlook.com
 * @version 0.1
 * @date 2016-09-12
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <syslog.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "http_proxy.h"
#include "logger.h"
#include "socketapi.h"

#define LOG_FILE "http_proxy.log"

typedef struct Server{
    struct sockaddr_in addr;
    short port; 
} Server;

typedef struct Client{
    struct sockaddr_in addr;
    int len;
} Client;


static int max_int(int a, int b)
{
    int ret;
    ret = a;
    if ( ret < 0 )
    {
        ret = b;
    }
    return ret;
}

static int  get_global_option(int argc, char **argv);
static int init_httpd_socket(int *sockfd);


int main(int argc, char **argv)
{
    FILE *logfile;
    int sockfd, clientfd; 
    Server server;
    Client client;
    fd_set rfds;
    int max_fd;
    struct timeval tv;
    int retval = -1;

    init_logger(LOG_FILE, logfile);
    Logger(LOG_INFO, 0, "httpd start...\n");

    init_httpd_socket(&sockfd);

    while (1)
    {
        client.len = sizeof (struct sockaddr);
        clientfd = Accept(sockfd, (struct sockaddr *)&client.addr, &client.len);
        Logger(LOG_INFO, 0, "A new connection accept!");

        while (1)
        {
            FD_ZERO(&rfds);
            FD_SET(clientfd, &rfds);
            max_fd = max_int(0, clientfd);

            tv.tv_sec = 1;
            tv.tv_usec = 0;
            retval = select(max_fd+1, &rfds, NULL, NULL, &tv);
            if ( retval == -1 )
            {
                Logger(LOG_ERR, 3, "ERROR: select failed");
                break;
            }
            else if ( retval == 0 )
            {
            //  printf("continue waiting!\n");
                continue;
            }
        }
    }

}

/**
 * @synopsis  init_httpd_socket, create a new socket for server 
 * @param sockfd
 * @returns  a new socket for server 
 */
static int init_httpd_socket(int *sockfd)
{
    struct sockaddr_in serv_addr;
    short port;

    Logger(LOG_INFO, 0, "init_http_socket...");

    port = htons(SERV_PORT); 

    *sockfd = Socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, 0, sizeof (serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port   = port;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    Bind(*sockfd, (struct sockaddr*)&serv_addr, sizeof (serv_addr));
    Listen(*sockfd, 6);
    return *sockfd;
}
MD XF
  • 7,860
  • 7
  • 40
  • 71
Windeal
  • 87
  • 1
  • 9
  • 6
    no text form, -1. – Sourav Ghosh Sep 14 '16 at 13:27
  • Please show your research/debugging effort so far. Please read [Ask] page first. – Sourav Ghosh Sep 14 '16 at 13:27
  • 2
    Not following site rules. VtC. No screenshots of text! – too honest for this site Sep 14 '16 at 13:33
  • Do not post your code as image. Please post your code as text directly in your question. Also please don't forget to post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeCAT Sep 14 '16 at 13:51
  • 1
    Smell like you have multiple definition of Logger in your code. Post the Logger function and the whole main.c file, at least. – LPs Sep 14 '16 at 13:52
  • How does `Logger` function is declared in `logger.h`? – Grzegorz Szpetkowski Sep 14 '16 at 14:27
  • Change the name in logger.h and logger.c of Logger function and see what happend. I mean: rename it to `new_Logger`. – LPs Sep 14 '16 at 14:45
  • I change it to my_log, but still not works. `1 main.c|66 col 22 warning| passing argument 2 of ‘my_log’ makes pointer from integer without a cast [-Wint-conversion] 2 logger.h|22 col 6 error| note: expected ‘int * (*)()’ but argument is of type ‘int’ ` – Windeal Sep 14 '16 at 14:51
  • #ifndef _LOGGER_H #define _LOGGER_H #ifndef _LOGGER_H #define _LOGGER_H #include #define DEBUG_ENABLE #ifdef DEBUG_ENABLE ...... #else #define DPRINT(fmt, ...) #endif extern char logfile_path[64]; FILE *logfile; void init_logger(char* logfile_path, FILE* file); void end_logger(FILE* file); void Logger(int priority, int errno, char* fmt, ...); #endif – Windeal Sep 14 '16 at 15:18
  • Hi all, I think I know the answer now. Thank you very much. – Windeal Sep 14 '16 at 15:49
  • I seldom ask questions here, I am a Chinese and my English is poor. Sorry to trouble you about not following site rules. – Windeal Sep 14 '16 at 15:52

1 Answers1

2

Now, I know the answer. The error should be in the declaration of Logger.

void Logger(int priority, int errno, char* fmt, ...);

The string 'errno' seems can not be a formal parameter. I repace it with 'Errno', then it works fine.

I presume that the string 'errno' may be a macro defined in errno.h. type 'man errno' may get more information.

Windeal
  • 87
  • 1
  • 9