如何检查来自perror的错误消息?

时间:2012-02-07 08:01:10

标签: c++ stdout stderr

我知道这很简单,但我无法得到它。

我正在使用OS-Ubuntu。以下代码是用mysql源代码编写的(在sql_parse.cc文件中)。我需要检查perror错误消息。我怎样才能收到这些消息? 我不认为这个错误消息会显示在运行mysql客户端/服务器的控制台中吗?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

{ 
string hashed;
int sockfd, n;
char buf[MAXDATASIZE], url[MAXDATASIZE], ack[6];
struct hostent *he;
struct sockaddr_in remote_addr;

if ((he=gethostbyname("localhost")) == NULL) { // get the host info
    perror("gethostbyname");
    exit(1);
    }

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
    }

remote_addr.sin_family = AF_INET; // host byte order
remote_addr.sin_port = htons(PORT); // short, network byte order
remote_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(remote_addr.sin_zero), '\0', 8); // zero the rest of the struct

if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1) {
    perror("connect");
    exit(1);
    }

请帮助我....

1 个答案:

答案 0 :(得分:0)

  1. perror功能是标准流之一。

  2. 它将输出提供给stderr。 您可以将stderr输出重定向到 Linux中的自定义位置(如文件或stdout-console)。

  3. 例如,在编译代码之后,让我们说一个程序文件创建为&#34; myprogram&#34;

    如果以这种方式运行此文件:

    ./myprogram 2>&1
    

    您可以在控制台中看到的错误错误

    对于您的特定情况,您需要阅读Mysql错误日志。在 Debian 中,Mysql配置文件说明了这一点:

      

    由于错误,日志记录将转到syslog   /etc/mysql/conf.d/mysqld_safe_syslog.cnf。

    这样,您可以通过阅读syslog文件来阅读这些错误。您可以通过文本编辑器或tail,less等来读取syslog:

    sudo gedit /var/log/syslog