LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2011, 11:47 AM   #1
jeremy28
Member
 
Registered: Sep 2009
Posts: 48

Rep: Reputation: 15
Problem in getting "log" using "UDPSocket" in a c++ program?!


Hi all;

I have a logger program (a .cpp file) that is intended to log all parts of my code (for debugging).

I've not written this code and want to ask you help me with a part of it, here is the necessary parts of logger code:

Code:
#include "PracticalSocket.h"
#include <time.h>

#define LOG_FILE	0
#define LOG_UDP		1

#ifndef NO_SOCKET
UDPSocket *loggerSocket = NULL;
#endif

FILE *fd = NULL;

void initLogger(int mode)
{
	if(mode == LOG_FILE)
	{
		fd = fopen("log.txt", "w");

		char buf[500];

		sprintf(buf, "%s\t\t\t\t%s\t%s\t%s\t%s\t%s\n", "Time", "PID", "TID", "FuncName", "LogType", "Parameters");

		fprintf(fd, buf);
	}

#ifndef NO_SOCKET
	else
	{
		loggerSocket = new UDPSocket(0);
	}
#endif

}


void logger(char* funcName, int logType, char* param)
{
	char buf[1500];

	time_t rawtime;
	struct tm * timeinfo;
	time(&rawtime);

	timeinfo = localtime ( &rawtime );

	char* temp = asctime(timeinfo);

	temp[strlen(temp) - 1] = 0;

	sprintf(buf, "%s\t%x\t%x\t%s\t%d\t%s\n", temp, getpid(), (unsigned int) pthread_self(), funcName, logType, param);

#ifndef NO_SOCKET

	if(loggerSocket != NULL)
	{
		loggerSocket->sendTo(buf, strlen(buf), "127.0.0.1", 10000);
	}
#endif

	if(fd != NULL)
	{
		fprintf(fd, buf);
		fflush(fd);
	}
}
As you see, I have 2 functions:

initLogger: To initialize logging task in a process.

logger: To report various pieces of program running (including "function name" and "return value") in log file - that is initialized in "initlogger".

My project consists of two parts: a service daemon program and a shared object (so) library to do some functions using the service that is running in the background!

I call the "initlogger" two times:

First time, in the service program with "LOG_FILE" mode (initlogger(LOG_FILE)), so that the "log.txt" file is created and
then the "logger" function is called multiple times to write the logs into the "log.txt".

Second time, in one of the library functions with "LOG_UDP" mode (initlogger(LOG_UDP)) and then the "logger" function is called several times again.

Currently, I've run the service and the "log.txt" is created and it logs everything correctly;

I have a test program to call the library's functions (that use the service) and want to see the related logs,

Now, My question is that how should I see the library logs?!

I mean, this line:
Code:
loggerSocket->sendTo(buf, strlen(buf), "127.0.0.1", 10000);
I think I should install a "log server" software on my computer (linux), but I don't know what software? and how should I set the port (10000) in it?

Unfortunately, I'm not familiar with "socket programming"!

The source of "PracticalSocket" is here:
Code:
http://cs.baylor.edu/~donahoo/practical/CSockets/practical/PracticalSocket.cpp
Could you help me with establishing a "log server" on my computer OR give me other solution to see the library logs please?

I want to use the "practical socket"(UDP) to log!

Best Regards.
 
Old 01-20-2011, 12:31 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You can send log messages all day long through a UDP socket, however if you do not have a peer application at the IP address ("127.0.0.1") and that is bound and willing to receive messages at port number (10000), then each and every one of the messages you send will end up in the "bit bucket". So unless you have some code that is calling recvFrom(), you have only completed 50% of the task of logging messages via UDP.

P.S. I hate being too critical of one's work, but aside from the usage of "new" in your code, the rest of it looks like C code, not C++.
 
1 members found this post helpful.
Old 01-21-2011, 02:00 AM   #3
jeremy28
Member
 
Registered: Sep 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dwhitney67 View Post
You can send log messages all day long through a UDP socket, however if you do not have a peer application at the IP address ("127.0.0.1") and that is bound and willing to receive messages at port number (10000), then each and every one of the messages you send will end up in the "bit bucket". So unless you have some code that is calling recvFrom(), you have only completed 50% of the task of logging messages via UDP.

P.S. I hate being too critical of one's work, but aside from the usage of "new" in your code, the rest of it looks like C code, not C++.
Thanks,

Could you introduce me a peer application to recieve the messages and how to use it?!
Because I've not already have such experience, my search on finding such applications would not be well!

As to your post, I understood that I should write a peer code to logger to use "recvFrom" function in it, likely with IP and Port, and receive the logs through it!

Is my understanding true?!!

If not, could you elaborate more please?

I don't want to be involved with such programming (not enough time) and an easy way like using a software would be better!
I only want to debug the program using logs;

Thanks again.

Last edited by jeremy28; 01-21-2011 at 02:01 AM.
 
Old 01-21-2011, 03:59 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Earlier you provided a URL with information concerning the Practical Socket; once you have downloaded the .h and .cpp files, you should avert your eyes from the .cpp file, and instead focus on the .h file. The latter describes the API for using the UDP Socket class.

A typical UDP server, that uses the PracticalSocket, would perform steps like:
Code:
#include "PracticalSocket.h"
#include <string>
#include <iostream>

int main(int argc, char** argv)
{
   unsigned short port = 10000;

   try
   {
      UDPSocket socket(port);

      for (;;)      // forever...
      {
         char           recvLogMsg[1024];
         std::string    fromAddr;
         unsigned short fromPort;

         int lenMsg = socket.recvMsg(recvLogMsg, sizeof(recvLogMsg) - 1, fromAddr, fromPort);

         if (lenMsg > 0)
         {
            recvLogMsg[lenMsg] = '\0';

            // do something with received message (ie. log it somewhere)...
         }
         else if (lenMsg < 0)
         {
            // error
         }
      }
   }
   catch (SocketException& se)
   {
      std::cerr << se.what() << std::endl;
      return -1;
   }
}

Last edited by dwhitney67; 01-21-2011 at 04:01 AM.
 
Old 01-21-2011, 07:50 AM   #5
jeremy28
Member
 
Registered: Sep 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dwhitney67 View Post
Earlier you provided a URL with information concerning the Practical Socket; once you have downloaded the .h and .cpp files, you should avert your eyes from the .cpp file, and instead focus on the .h file. The latter describes the API for using the UDP Socket class.

A typical UDP server, that uses the PracticalSocket, would perform steps like:
Code:
#include "PracticalSocket.h"
#include <string>
#include <iostream>

int main(int argc, char** argv)
{
   unsigned short port = 10000;

   try
   {
      UDPSocket socket(port);

      for (;;)      // forever...
      {
         char           recvLogMsg[1024];
         std::string    fromAddr;
         unsigned short fromPort;

         int lenMsg = socket.recvMsg(recvLogMsg, sizeof(recvLogMsg) - 1, fromAddr, fromPort);

         if (lenMsg > 0)
         {
            recvLogMsg[lenMsg] = '\0';

            // do something with received message (ie. log it somewhere)...
         }
         else if (lenMsg < 0)
         {
            // error
         }
      }
   }
   catch (SocketException& se)
   {
      std::cerr << se.what() << std::endl;
      return -1;
   }
}
Hi and Thank you very much;

By building this program, I will have a process that listens to the received messages from logger program continuously and writes them in a file for example.

Now, I have another questions:
Beforehand, sorry for my clumsily questions;

I will send the log messages on the same computer (namely, 127.0.0.1) at port 10000.
But I don't know which value should I pass as the forth arqument of "socket.recvFrom()" function that is, "fromPort"?
(This question triggered because the lack of knowledge about network & socket programming, ports, and ... sorry!!)

I think I should pass "127.0.0.1" (the same computer) string to "fromAddr" parameter, but what about "fromPort" value?

I mean, how should I define a port number for logger program to be known to your provided program?

And the other question:
Apart from using this program and getting log on linux, I also have my project binaries (service & library)running on Windows (with Windows-specific APIs and ...) so I don't want to compile it again!

If I build your program, it will run on windows as an always running process and log the messages.

Also, I've downloaded many sofwares like "z-log.exe", "log2stats.exe", etc to learn using them with my logger program. Is it possible to change their mode to UDP and define an appropriate IP and Port to listen the sent messages from logger and inscribe them into their database?

So far, I've not used a log server software and I'm very eager to try it too...

I'll greatly appreciate you if share me your knowledge again.

TIA.
 
Old 01-21-2011, 08:01 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by jeremy28 View Post
I will send the log messages on the same computer (namely, 127.0.0.1) at port 10000.
But I don't know which value should I pass as the forth arqument of "socket.recvFrom()" function that is, "fromPort"?
(This question triggered because the lack of knowledge about network & socket programming, ports, and ... sorry!!)

I think I should pass "127.0.0.1" (the same computer) string to "fromAddr" parameter, but what about "fromPort" value?

I mean, how should I define a port number for logger program to be known to your provided program?
You don't need to specify the fromAddr and fromPort information when calling recvFrom(); that information is returned to you after the successful completion of the function call. The way I coded it in the example provided earlier is how you would set up the variables and the function call. Whether you decide to use the fromAddr/fromPort values that are returned is up to you.

Quote:
Originally Posted by jeremy28 View Post
And the other question:
Apart from using this program and getting log on linux, I also have my project binaries (service & library)running on Windows (with Windows-specific APIs and ...) so I don't want to compile it again!

If I build your program, it will run on windows as an always running process and log the messages.

Also, I've downloaded many sofwares like "z-log.exe", "log2stats.exe", etc to learn using them with my logger program. Is it possible to change their mode to UDP and define an appropriate IP and Port to listen the sent messages from logger and inscribe them into their database?
I cannot help with Windows, for I do not develop s/w for that platform, nor do I ever wish to.

If you are not able to develop the s/w applications on your own, you may want to consider hiring a freelance s/w developer. Just make sure that you provide the developer with the specific requirements for your project.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
The "Log out" and "Lock screen" actions cannot be executed through keyboard shortcuts Snood Linux - Desktop 0 04-22-2009 09:30 AM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
New SQUID user: How to clear the "access.log" and "store.log" automatically? yuzuohong Linux - Networking 2 12-02-2006 05:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration