Search code examples
cudpsyslogsendto

what wrong with "<>" missing


I wrote a program to send syslog to remote sever.Part of my code is below:

sprintf(syslogBuf,"<%d>LogTime=\"%s\";user=\"%s\";IP=\"%s\";Sip=\"%s\";OpType=\"%s\";OpResult=\"%d\";OpText=\"%s %s\"",
        pri,p[0],p[1],IP,p[2],p[3],flag,p[1],p[3]);

And I used sendto() to send syslogBuf to remote sylog sever.But what the sever received is :

LogTime="2015-10-20 14:33:57";user="root";IP="127.0.1.12";Sip="127.0.1.14";OpType="show meminfo";OpResult="0";OpText="root show meminfo"

why did <%d> miss? the sending code is below:

if(UdpSendData(sockfd,syslogBuf,strlen(syslogBuf),syslogServer,SYSLOGPORT) == -1);
perror("send failed");

UdpSendData:

int UdpSendData(int sockfd,const char *buf, UINT32 dataLen, char *remoteIP, int remotePort)
{

struct sockaddr_in RemoteAddr;
int len;

if( sockfd <= -1)
{
    printf("%s send fail,sock error.\n",__FUNCTION__);
    return -1;
}
if((buf == NULL)||(remoteIP==NULL))
{
    printf("%s send fail,buf error or remote ip error.\n",__FUNCTION__);
    return -1;
}
//printf("udp send remote ip %s port %d.\n",remoteIP,remotePort);

memset(&RemoteAddr,0,sizeof(RemoteAddr));  
RemoteAddr.sin_family = AF_INET;
RemoteAddr.sin_port = htons( remotePort );
RemoteAddr.sin_addr.s_addr = inet_addr( remoteIP );
int addrLen = sizeof(RemoteAddr);

len = sendto(sockfd, buf, dataLen, 0, (struct sockaddr*)&RemoteAddr, addrLen); 

if  (len <= 0)
{   
    printf("%s udp send to %s %d fail.\n",__FUNCTION__,remoteIP,remotePort);
    return -1;
}   
return len;

}


Solution

  • <%d>
    

    Is sent, but syslog interprets it as a priority tag rather then part of the message. You can simply instead use [%d] or (%d).