LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 04-09-2019, 12:09 PM   #1
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
Sharing log file among classes


Centos Linux
I have a class that instantiates other classes and want all to write to a single log file. I am unable to find the correct syntax and am not certain that this can easily be done. My searches for topics such as sharing a log file lead to results on piping outputs rather than from within an application.

Below is the basic concept. Can this be done? Maybe some search hints to find what others have discussed?

file a.h contains
Code:
class a{ ...
public
   a( ); 
private:
   std::ofstream m_logs; 
...}; // end of class a
file a.cpp contains

Code:
a::a() {
m_logs.open( "/tmp/a_logs.txt" );
m_logs << "write a stuff"
b = new class_b( std::ofstream & log_file );
...} // end of a constructor
file b.h contains
Code:
class b{ ...
public:
   b( std::ofstream & log_file ); // compile error here
private:
   void x( );
   std::ofstream m_logs;
... } // end class b
The text from the compile error is:
Quote:
error: could not convert '0l' from 'long int' to 'std :: ofstream& {aka std :: basic_ofstream<char>&}'
(side note: added spaces around the some of the colons to prevent emoticons)

I do not understand why it declares that std :: ofstream is a long integer;

file b.cpp contins

Code:
b :: b( std::ofstream & log_file ) {

   m_logs = log_file; // ?
   m_logs << "write b::b stuff" << std::endl;
   x();
...} // end constructor b
b :: x(){ ...
   m_logs << "write b :: x stuff" << std::endl; 
... } // end function b :: x()
 
Old 04-11-2019, 07:12 PM   #2
tyler2016
Member
 
Registered: Sep 2018
Distribution: Debian, CentOS, FreeBSD
Posts: 243

Rep: Reputation: Disabled
I don't see where in a.cpp you pass the constructor an ofstream instance? Maybe replace:

Code:
b = new class_b( std::ofstream & log_file );
with:

Code:
b = new class_b( std::ofstream & m_logs );
My first thought was: Is there some reason you can't just log to the system logger and configure it to write everything from your application to a specific file? I am sysad at my day job and would much rather an application log to the syslog than it's own file.
 
Old 04-17-2019, 11:23 AM   #3
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by tyler2016 View Post
... I am sysad at my day job and would much rather an application log to the syslog than it's own file.
When developing an app and trying to find the defects I have put in, I don't want to write copious quantities of debug info to any system wide log. Thanks for the tip and I am still working on it.
 
Old 04-17-2019, 12:27 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
without seeing the real code hard to say anything. If I need to guess that log_file is already declared somewhere as long int.
 
Old 04-18-2019, 07:06 AM   #5
tyler2016
Member
 
Registered: Sep 2018
Distribution: Debian, CentOS, FreeBSD
Posts: 243

Rep: Reputation: Disabled
I am pretty rusty on my C++, it has been 3 years or so since I have written any C++. My recollection of new, constructors, etc is a bit foggy. I did a little bit of brushing up and found my comment on how the constructor is called is incorrect.

I think pan64 is probably correct. Try this:

Code:
cd /path/to/src/tree
find . -type f -exec grep -l log_file {} \;
This will show you all files with log_file in it within your source tree.
 
Old 04-18-2019, 08:07 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You don't "share" across classes.

You define a separate log file class which services the log file and you provide public methods to access it.
 
Old 04-18-2019, 08:56 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
@bkelly,

I see that you've now started a new thread asking for code changes to what you've been trying to accomplish writing to a log file.

Building a TID based file name

While I understand that you have modified your question, it is clearly a continuation of this question where you've learned additional information, and are trying some next steps.

Both of these topics are more suitable for the Programming forum, so I'm closing this one and moving your new question to Programming.

I may have a suggestion for that thread, however it won't be Qt specific because I no longer have a development system with Qt fully installed.

In the future, please consider marking this thread as solved, posting a link to your updated question here as a reference, and also consider which forums may be better for each question subject.
 
  


Closed Thread



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
How to log internal-sftp chroot jailed users access log to /var/log/sftp.log file LittleMaster Linux - Server 0 09-04-2018 03:45 PM
Singleton Classes in PHP: Not like other OOP singleton classes... rm_-rf_windows Programming 4 04-19-2012 11:13 AM
Class inheritance terminology: super-classes or base classes? 2ck Programming 6 07-20-2011 10:17 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
how to configure the printer for sharing among LAN systems? malikguna Linux - Networking 1 08-07-2004 09:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:23 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