www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is best way to get see function from separate file

reply Suliman <evermind live.ru> writes:
I have got logger instance in App.d

void main()
{
FileLogger fLogger = new FileLogger("ErrorLog.txt");
Apr 10 2016
parent reply Suliman <evermind live.ru> writes:
Sorry for wrong posting!

I have got logger instance in App.d

void main()
{
...
  FileLogger fLogger = new FileLogger("ErrorLog.txt");
  foo();
}

utils.d:
foo()
{
// I need logging here
}

Also I have file utils.d that include stand-alone functions that 
is not in classes. In one of them I need to implement logging.

What is the best way to do it. I see only two way -- create new 
Loggining instance. And second -- to import App.d as module, 
because without importing I would not able to see Logger instance.

But both of this way is look ugly. Is there any best solution?
Apr 10 2016
parent reply Jonathan Villa <jv_vortex msn.com> writes:
On Sunday, 10 April 2016 at 18:26:57 UTC, Suliman wrote:
 Sorry for wrong posting!

 I have got logger instance in App.d

 void main()
 {
 ...
  FileLogger fLogger = new FileLogger("ErrorLog.txt");
  foo();
 }

 utils.d:
 foo()
 {
 // I need logging here
 }

 Also I have file utils.d that include stand-alone functions 
 that is not in classes. In one of them I need to implement 
 logging.

 What is the best way to do it. I see only two way -- create new 
 Loggining instance. And second -- to import App.d as module, 
 because without importing I would not able to see Logger 
 instance.

 But both of this way is look ugly. Is there any best solution?
You could pass an argument of type FileLogger (probably better a pointer?) foo ( FileLogger log ) { } Other whay is to leave FileLogger instance in a separated module: logger.d public static FileLogger fLogger; App.d import logger; //the module void main() { // generate instance logger = new FileLogger("ErrorLog.txt"); } utils.d import logger; // the module foo () { fLogger... } I cannot think in other ways. JV
Apr 10 2016
parent reply Jonathan Villa <jv_vortex msn.com> writes:
On Sunday, 10 April 2016 at 18:36:19 UTC, Jonathan Villa wrote:
 On Sunday, 10 April 2016 at 18:26:57 UTC, Suliman wrote:

 Other whay is to leave FileLogger instance in a separated 
 module:
 logger.d
 public static FileLogger fLogger;

 App.d
 import logger; //the module
 void main()
 {
     // generate instance
     logger = new FileLogger("ErrorLog.txt");
 }

 utils.d
 import logger; // the module

 foo ()
 {
     fLogger...
 }

 I cannot think in other ways.

 JV
FIXING MAIN() void main() { // generate instance fLogger = new FileLogger("ErrorLog.txt"); }
Apr 10 2016
parent reply Suliman <evermind live.ru> writes:
You could pass an argument of type FileLogger (probably better a 
pointer?)
foo ( FileLogger log )
{ }
I like it. Am i right understand that it prevent creation unneeded of new instance of logger? And what problems I can get if I will create new instance of logger in every stand alone function? Just resource overusage?
Apr 10 2016
parent Jonathan Villa <jv_vortex msn.com> writes:
On Sunday, 10 April 2016 at 18:57:45 UTC, Suliman wrote:
 I like it. Am i right understand that it prevent creation 
 unneeded of new instance of logger?
No, you need to pass a valid instance in foo(...), It should have been created before the call to foo(...). I prefer the second way (separate file of the fLogger variable) so you can use any time, everywhere just adding the import using just one instance. If you want create an instance every time you get to foo() and without the need of an argument, just import the logger library: import std.experimental.logger; //here foo() { auto log = new FileLogger("ErrorLog.txt"); ... destroy(log); }
 And what problems I can get if I will create new instance of 
 logger in every stand alone function? Just resource overusage?
I don't know how exactly FileLogger works, if it need some kind of close() function like normal files or it open the file ... write on it ... and after that it close the file. But the basic thing is ... yes, allocatin and deallocating an instance everytime you are going to use it. JV.
Apr 10 2016