Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 10, 2016 What is best way to get see function from separate file | ||||
---|---|---|---|---|
| ||||
I have got logger instance in App.d void main() { FileLogger fLogger = new FileLogger("ErrorLog.txt"); |
April 10, 2016 Re: What is best way to get see function from separate file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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? |
April 10, 2016 Re: What is best way to get see function from separate file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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
|
April 10, 2016 Re: What is best way to get see function from separate file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Villa | 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");
}
|
April 10, 2016 Re: What is best way to get see function from separate file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Villa | >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?
|
April 10, 2016 Re: What is best way to get see function from separate file | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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. |
Copyright © 1999-2021 by the D Language Foundation