Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sharedLog assignment. #8995

Merged
merged 4 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions std/logger/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ logger by the user, the default logger's log level is LogLevel.info.

Example:
-------------
sharedLog = new FileLogger(yourFile);
sharedLog = new shared FileLogger(yourFile);
-------------
The example sets a new `FileLogger` as new `sharedLog`.

Expand All @@ -1450,7 +1450,7 @@ writing `sharedLog`.
The default `Logger` is thread-safe.
-------------
if (sharedLog !is myLogger)
sharedLog = new myLogger;
sharedLog = new shared myLogger;
-------------
*/
@property shared(Logger) sharedLog() @safe
Expand Down
16 changes: 13 additions & 3 deletions std/logger/filelogger.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FileLogger : Logger
auto l3 = new FileLogger("logFile", LogLevel.fatal, CreateFolder.yes);
-------------
*/
this(const string fn, const LogLevel lv = LogLevel.all) @safe
this(this This)(const string fn, const LogLevel lv = LogLevel.all)
{
this(fn, lv, CreateFolder.yes);
}
Expand All @@ -63,7 +63,7 @@ class FileLogger : Logger
auto l2 = new FileLogger(file, LogLevel.fatal);
-------------
*/
this(const string fn, const LogLevel lv, CreateFolder createFileNameFolder) @safe
this(this This)(const string fn, const LogLevel lv, CreateFolder createFileNameFolder)
{
import std.file : exists, mkdirRecurse;
import std.path : dirName;
Expand All @@ -80,7 +80,8 @@ class FileLogger : Logger
" created in '", d,"' could not be created."));
}

this.file_.open(this.filename, "a");
// Cast away `shared` when the constructor is inferred shared.
() @trusted { (cast() this.file_).open(this.filename, "a"); }();
veelo marked this conversation as resolved.
Show resolved Hide resolved
}

/** A constructor for the `FileLogger` Logger that takes a reference to
Expand Down Expand Up @@ -270,3 +271,12 @@ class FileLogger : Logger
assert(tl !is null);
stdThreadLocalLog.logLevel = LogLevel.all;
}

@safe unittest
{
// we don't need to actually run the code, only make sure
// it compiles
static _() {
auto l = new shared FileLogger("");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what exactly does this test if we assign this to a new variable that should always work anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is @atilaneves ' line, but, as I understand it, it tests that the templated constructor works as a shared constructor.

}
}
2 changes: 1 addition & 1 deletion std/logger/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ using the property called `sharedLog`. This property is a reference to the
current default `Logger`. This reference can be used to assign a new
default `Logger`.
-------------
sharedLog = new FileLogger("New_Default_Log_File.log");
sharedLog = new shared FileLogger("New_Default_Log_File.log");
-------------

Additional `Logger` can be created by creating a new instance of the
Expand Down
Loading