diff --git a/source/dub/commandline.d b/source/dub/commandline.d index 15605d4..ccdd8af 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -14,7 +14,7 @@ import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import dub.package_; import dub.packagemanager; import dub.packagesuppliers; diff --git a/source/dub/compilers/compiler.d b/source/dub/compilers/compiler.d index b8e2107..a8dacd0 100644 --- a/source/dub/compilers/compiler.d +++ b/source/dub/compilers/compiler.d @@ -13,7 +13,7 @@ import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import std.algorithm; import std.array; diff --git a/source/dub/compilers/dmd.d b/source/dub/compilers/dmd.d index ba49e90..4224518 100644 --- a/source/dub/compilers/dmd.d +++ b/source/dub/compilers/dmd.d @@ -11,7 +11,7 @@ import dub.compilers.utils; import dub.internal.utils; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import std.algorithm; import std.array; diff --git a/source/dub/compilers/gdc.d b/source/dub/compilers/gdc.d index cab4759..82216df 100644 --- a/source/dub/compilers/gdc.d +++ b/source/dub/compilers/gdc.d @@ -11,7 +11,7 @@ import dub.compilers.utils; import dub.internal.utils; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import std.algorithm; import std.array; diff --git a/source/dub/compilers/ldc.d b/source/dub/compilers/ldc.d index a850907..8487609 100644 --- a/source/dub/compilers/ldc.d +++ b/source/dub/compilers/ldc.d @@ -11,7 +11,7 @@ import dub.compilers.utils; import dub.internal.utils; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import std.algorithm; import std.array; diff --git a/source/dub/compilers/utils.d b/source/dub/compilers/utils.d index 86bde90..19b1419 100644 --- a/source/dub/compilers/utils.d +++ b/source/dub/compilers/utils.d @@ -10,7 +10,7 @@ import dub.compilers.buildsettings; import dub.platform : BuildPlatform, archCheck, compilerCheck, platformCheck; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import std.algorithm : canFind, endsWith, filter; diff --git a/source/dub/dependency.d b/source/dub/dependency.d index 16210db..27288ba 100644 --- a/source/dub/dependency.d +++ b/source/dub/dependency.d @@ -13,7 +13,7 @@ import dub.internal.vibecompat.inet.path; import dub.package_; import dub.semver; -import dub.logging; +import dub.internal.logging; import std.algorithm; import std.array; diff --git a/source/dub/dependencyresolver.d b/source/dub/dependencyresolver.d index 527b632..5cb2e13 100644 --- a/source/dub/dependencyresolver.d +++ b/source/dub/dependencyresolver.d @@ -8,7 +8,7 @@ module dub.dependencyresolver; import dub.dependency; -import dub.logging; +import dub.internal.logging; import std.algorithm : all, canFind, filter, map, sort; import std.array : appender, array, join; diff --git a/source/dub/dub.d b/source/dub/dub.d index 529b685..108f124 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -14,7 +14,7 @@ import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.url; -import dub.logging; +import dub.internal.logging; import dub.package_; import dub.packagemanager; import dub.packagesuppliers; diff --git a/source/dub/generators/build.d b/source/dub/generators/build.d index 272f93c..c87511b 100644 --- a/source/dub/generators/build.d +++ b/source/dub/generators/build.d @@ -13,7 +13,7 @@ import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import dub.package_; import dub.packagemanager; import dub.project; diff --git a/source/dub/generators/cmake.d b/source/dub/generators/cmake.d index 97e67bd..3be3d62 100644 --- a/source/dub/generators/cmake.d +++ b/source/dub/generators/cmake.d @@ -11,7 +11,7 @@ import dub.generators.generator; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import dub.project; import std.algorithm: map, uniq; diff --git a/source/dub/generators/generator.d b/source/dub/generators/generator.d index e3e9571..c428df4 100644 --- a/source/dub/generators/generator.d +++ b/source/dub/generators/generator.d @@ -14,7 +14,7 @@ import dub.generators.visuald; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import dub.package_; import dub.packagemanager; import dub.project; diff --git a/source/dub/generators/sublimetext.d b/source/dub/generators/sublimetext.d index fd873eb..d46fbd9 100644 --- a/source/dub/generators/sublimetext.d +++ b/source/dub/generators/sublimetext.d @@ -11,7 +11,7 @@ import dub.generators.generator; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import dub.packagemanager; import dub.project; diff --git a/source/dub/generators/visuald.d b/source/dub/generators/visuald.d index f63ffc7..3e90d6a 100644 --- a/source/dub/generators/visuald.d +++ b/source/dub/generators/visuald.d @@ -11,7 +11,7 @@ import dub.generators.generator; import dub.internal.utils; import dub.internal.vibecompat.core.file; -import dub.logging; +import dub.internal.logging; import dub.package_; import dub.packagemanager; import dub.project; diff --git a/source/dub/init.d b/source/dub/init.d index 160449b..b4c887c 100644 --- a/source/dub/init.d +++ b/source/dub/init.d @@ -8,7 +8,7 @@ module dub.init; import dub.internal.vibecompat.core.file; -import dub.logging; +import dub.internal.logging; import dub.package_ : PackageFormat, packageInfoFiles, defaultPackageFilename; import dub.recipe.packagerecipe; import dub.dependency; diff --git a/source/dub/internal/logging.d b/source/dub/internal/logging.d new file mode 100644 index 0000000..cc068c0 --- /dev/null +++ b/source/dub/internal/logging.d @@ -0,0 +1,384 @@ +/** + Handles all the console output of the Dub package manager, by providing useful + methods for handling colored text. The module also disables colors when stdout + and stderr are not a TTY in order to avoid ASCII escape sequences in piped + output. The module can autodetect and configure itself in this regard by + calling initLogging() at the beginning of the program. But, whether to color + text or not can also be set manually with setLoggingColorsEnabled(bool). + + The output for the log levels error, warn and info is formatted like this: + + " " + '----------' + fixed width + + the "tag" part can be colored (most oftenly will be) and always has a fixed + width, which is defined as a const at the beginning of this module. + + The output for the log levels debug and diagnostic will be just the plain + string. + + There are some default tag string and color values for some logging levels: + - warn: "Warning", yellow bold + - error: "Error", red bold + + Actually, for error and warn levels, the tag color is fixed to the ones listed + above. + + Also, the default tag string for the info level is "" (the empty string) and + the default color is white (usually it's manually set when calling logInfo + with the wanted tag string, but this allows to just logInfo("text") without + having to worry about the tag if it's not needed). + + Usage: + After initializing the logging module with initLogging(), the functions + logDebug(..), logDiagnostic(..), logInfo(..), logWarning(..) and logError(..) + can be used to print log messages. Whether the messages are printed on stdout + or stderr depends on the log level (warning and error go to stderr). + The log(..) function can also be used. Check the signature and documentation + of the functions for more information. + + The minimum log level to print can be configured using setLogLevel(..), + and whether to color outputted text or not can be set with + setLoggingColorsEnabled(..) + + The color(str, color) function can be used to color text within a log + message, for instance like this: + + logInfo("Tag", Color.green, "My %s message", "colored".color(Color.red)) + + Copyright: © 2018 Giacomo De Lazzari + License: Subject to the terms of the MIT license, as written in the included LICENSE file. + Authors: Giacomo De Lazzari +*/ + +module dub.internal.logging; + +import std.stdio; +import std.array; +import std.format; +import std.string; + +import dub.internal.colorize : fg, mode; + +/** + An enum listing possible colors for terminal output, useful to set the color + of a tag. Re-exported from d-colorize in dub.internal.colorize. See the enum + definition there for a list of possible values. +*/ +public alias Color = fg; + +/** + An enum listing possible text "modes" for terminal output, useful to set the + text to bold, underline, blinking, etc... + Re-exported from d-colorize in dub.internal.colorize. See the enum definition + there for a list of possible values. +*/ +public alias Mode = mode; + +/// The tag width in chars, defined as a constant here +private const int TAG_WIDTH = 12; + +/// Possible log levels supported +enum LogLevel { + debug_, + diagnostic, + info, + warn, + error, + none +} + +// The current minimum log level to be printed +private shared LogLevel _minLevel = LogLevel.info; + +/* + Whether to print text with colors or not, defaults to true but will be set + to false in initLogging() if stdout or stderr are not a TTY (which means the + output is probably being piped and we don't want ASCII escape chars in it) +*/ +private shared bool _printColors = true; + +// isatty() is used in initLogging() to detect whether or not we are on a TTY +extern (C) int isatty(int); + +/** + This function must be called at the beginning for the program, before any + logging occurs. It will detect whether or not stdout/stderr are a console/TTY + and will consequently disable colored output if needed. + + Forgetting to call the function will result in ASCII escape sequences in the + piped output, probably an undesiderable thing. +*/ +void initLogging() +{ + import core.stdc.stdio; + + // Initially enable colors, we'll disable them during this functions if we + // find any reason to + _printColors = true; + + // The following stuff depends on the platform + version (Windows) + { + version (CRuntime_DigitalMars) + { + if (!isatty(core.stdc.stdio.stdout._file) || + !isatty(core.stdc.stdio.stderr._file)) + _printColors = false; + } + else version (CRuntime_Microsoft) + { + if (!isatty(fileno(core.stdc.stdio.stdout)) || + !isatty(fileno(core.stdc.stdio.stderr))) + _printColors = false; + } + else + _printColors = false; + } + else version (Posix) + { + import core.sys.posix.unistd; + + if (!isatty(STDERR_FILENO) || !isatty(STDOUT_FILENO)) + _printColors = false; + } +} + +/// Sets the minimum log level to be printed +void setLogLevel(LogLevel level) nothrow +{ + _minLevel = level; +} + +/// Gets the minimum log level to be printed +LogLevel getLogLevel() +{ + return _minLevel; +} + +/// Set whether to print colors or not +void setLoggingColorsEnabled(bool enabled) +{ + _printColors = enabled; +} + +/** + Shorthand function to log a message with debug/diagnostic level, no tag string + or tag color required (since there will be no tag). + + Params: + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logDebug(T...)(string fmt, lazy T args) nothrow +{ + log(LogLevel.debug_, false, "", Color.init, fmt, args); +} + +/// ditto +void logDiagnostic(T...)(string fmt, lazy T args) nothrow +{ + log(LogLevel.diagnostic, false, "", Color.init, fmt, args); +} + +/** + Shorthand function to log a message with info level, with custom tag string + and tag color. + + Params: + tag = The string the tag at the beginning of the line should contain + tagColor = The color the tag string should have + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logInfo(T...)(string tag, Color tagColor, string fmt, lazy T args) nothrow +{ + log(LogLevel.info, false, tag, tagColor, fmt, args); +} + +/** + Shorthand function to log a message with info level, this version prints an + empty tag automatically (which is different from not having a tag - in this + case there will be an identation of TAG_WIDTH chars on the left anyway). + + Params: + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logInfo(T...)(string fmt, lazy T args) nothrow if (!is(T[0] : Color)) +{ + log(LogLevel.info, false, "", Color.init, fmt, args); +} + +/** + Shorthand function to log a message with info level, this version doesn't + print a tag at all, it effectively just prints the given string. + + Params: + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logInfoNoTag(T...)(string fmt, lazy T args) nothrow if (!is(T[0] : Color)) +{ + log(LogLevel.info, true, "", Color.init, fmt, args); +} + +/** + Shorthand function to log a message with warning level, with custom tag string. + The tag color is fixed to yellow. + + Params: + tag = The string the tag at the beginning of the line should contain + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logWarnTag(T...)(string tag, string fmt, lazy T args) nothrow +{ + log(LogLevel.warn, false, tag, Color.yellow, fmt, args); +} + +/** + Shorthand function to log a message with warning level, using the default + tag "Warning". The tag color is also fixed to yellow. + + Params: + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logWarn(T...)(string fmt, lazy T args) nothrow +{ + log(LogLevel.warn, false, "Warning", Color.yellow, fmt, args); +} + +/** + Shorthand function to log a message with error level, with custom tag string. + The tag color is fixed to red. + + Params: + tag = The string the tag at the beginning of the line should contain + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logErrorTag(T...)(string tag, string fmt, lazy T args) nothrow +{ + log(LogLevel.error, false, tag, Color.red, fmt, args); +} + +/** + Shorthand function to log a message with error level, using the default + tag "Error". The tag color is also fixed to red. + + Params: + level = The log level for the logged message + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void logError(T...)(string fmt, lazy T args) nothrow +{ + log(LogLevel.error, false, "Error", Color.red, fmt, args); +} + +/** + Log a message with the specified log level and with the specified tag string + and color. If the log level is debug or diagnostic, the tag is not printed + thus the tag string and tag color will be ignored. If the log level is error + or warning, the tag will be in bold text. Also the tag can be disabled (for + any log level) by passing true as the second argument. + + Params: + level = The log level for the logged message + disableTag = Setting this to true disables the tag, no matter what + tag = The string the tag at the beginning of the line should contain + tagColor = The color the tag string should have + fmt = See http://dlang.org/phobos/std_format.html#format-string +*/ +void log(T...)( + LogLevel level, + bool disableTag, + string tag, + Color tagColor, + string fmt, + lazy T args +) nothrow +{ + if (level < _minLevel) + return; + + auto hasTag = true; + if (level <= LogLevel.diagnostic) + hasTag = false; + if (disableTag) + hasTag = false; + + auto boldTag = false; + if (level >= LogLevel.warn) + boldTag = true; + + try + { + string result = format(fmt, args); + + if (hasTag) + result = tag.rightJustify(TAG_WIDTH, ' ').color(tagColor, boldTag ? Mode.bold : Mode.init) ~ " " ~ result; + + import dub.internal.colorize : cwrite; + + File output = (level <= LogLevel.info) ? stdout : stderr; + + if (output.isOpen) + { + output.cwrite(result, "\n"); + output.flush(); + } + } + catch (Exception e) + { + debug assert(false, e.msg); + } +} + +/** + Colors the specified string with the specified color. The function is used to + print colored text within a log message. The function also checks whether + color output is enabled or disabled (when not outputting to a TTY) and, in the + last case, just returns the plain string. This allows to use it like so: + + logInfo("Tag", Color.green, "My %s log message", "colored".color(Color.red)); + + without worring whether or not colored output is enabled or not. + + Also a mode can be specified, such as bold/underline/etc... + + Params: + str = The string to color + color = The color to apply + mode = An optional mode, such as bold/underline/etc... +*/ +string color(const string str, const Color c, const Mode m = Mode.init) +{ + import dub.internal.colorize; + + if (_printColors) + return dub.internal.colorize.color(str, c, bg.init, m); + else + return str; +} + +/** + This function is the same as the above one, but just accepts a mode. + It's useful, for instance, when outputting bold text without changing the + color. + + Params: + str = The string to color + mode = The mode, such as bold/underline/etc... +*/ +string color(const string str, const Mode m = Mode.init) +{ + import dub.internal.colorize; + + if (_printColors) + return dub.internal.colorize.color(str, fg.init, bg.init, m); + else + return str; +} diff --git a/source/dub/internal/utils.d b/source/dub/internal/utils.d index 16fdfdf..bf27d19 100644 --- a/source/dub/internal/utils.d +++ b/source/dub/internal/utils.d @@ -12,7 +12,7 @@ import dub.internal.vibecompat.inet.url; import dub.compilers.buildsettings : BuildSettings; import dub.version_; -import dub.logging; +import dub.internal.logging; import core.time : Duration; import std.algorithm : canFind, startsWith; diff --git a/source/dub/internal/vibecompat/core/file.d b/source/dub/internal/vibecompat/core/file.d index d1b55e6..15ee255 100644 --- a/source/dub/internal/vibecompat/core/file.d +++ b/source/dub/internal/vibecompat/core/file.d @@ -9,7 +9,7 @@ public import dub.internal.vibecompat.inet.url; -import dub.logging; +import dub.internal.logging; import std.conv; import core.stdc.stdio; diff --git a/source/dub/internal/vibecompat/core/log.d b/source/dub/internal/vibecompat/core/log.d index 404e4b5..5c65737 100644 --- a/source/dub/internal/vibecompat/core/log.d +++ b/source/dub/internal/vibecompat/core/log.d @@ -1,5 +1,5 @@ -/** +deprecated("Please use dub.logging") - This module contained logging functions, but has been replaced by dub.logging +/** +deprecated("Please use dub.internal.logging") + This module contained logging functions, but has been replaced by dub.internal.logging which provides colored output and other facilities. Copyright: © 2012 rejectedsoftware e.K., © 2018 Giacomo De Lazzari @@ -7,8 +7,8 @@ Authors: Sönke Ludwig, Giacomo De Lazzari */ -deprecated("Please use dub.logging") +deprecated("Please use dub.internal.logging") module dub.internal.vibecompat.core.log; -public import dub.logging; +public import dub.internal.logging; diff --git a/source/dub/logging.d b/source/dub/logging.d deleted file mode 100644 index 8da4ee6..0000000 --- a/source/dub/logging.d +++ /dev/null @@ -1,384 +0,0 @@ -/** - Handles all the console output of the Dub package manager, by providing useful - methods for handling colored text. The module also disables colors when stdout - and stderr are not a TTY in order to avoid ASCII escape sequences in piped - output. The module can autodetect and configure itself in this regard by - calling initLogging() at the beginning of the program. But, whether to color - text or not can also be set manually with setLoggingColorsEnabled(bool). - - The output for the log levels error, warn and info is formatted like this: - - " " - '----------' - fixed width - - the "tag" part can be colored (most oftenly will be) and always has a fixed - width, which is defined as a const at the beginning of this module. - - The output for the log levels debug and diagnostic will be just the plain - string. - - There are some default tag string and color values for some logging levels: - - warn: "Warning", yellow bold - - error: "Error", red bold - - Actually, for error and warn levels, the tag color is fixed to the ones listed - above. - - Also, the default tag string for the info level is "" (the empty string) and - the default color is white (usually it's manually set when calling logInfo - with the wanted tag string, but this allows to just logInfo("text") without - having to worry about the tag if it's not needed). - - Usage: - After initializing the logging module with initLogging(), the functions - logDebug(..), logDiagnostic(..), logInfo(..), logWarning(..) and logError(..) - can be used to print log messages. Whether the messages are printed on stdout - or stderr depends on the log level (warning and error go to stderr). - The log(..) function can also be used. Check the signature and documentation - of the functions for more information. - - The minimum log level to print can be configured using setLogLevel(..), - and whether to color outputted text or not can be set with - setLoggingColorsEnabled(..) - - The color(str, color) function can be used to color text within a log - message, for instance like this: - - logInfo("Tag", Color.green, "My %s message", "colored".color(Color.red)) - - Copyright: © 2018 Giacomo De Lazzari - License: Subject to the terms of the MIT license, as written in the included LICENSE file. - Authors: Giacomo De Lazzari -*/ - -module dub.logging; - -import std.stdio; -import std.array; -import std.format; -import std.string; - -import dub.internal.colorize : fg, mode; - -/** - An enum listing possible colors for terminal output, useful to set the color - of a tag. Re-exported from d-colorize in dub.internal.colorize. See the enum - definition there for a list of possible values. -*/ -public alias Color = fg; - -/** - An enum listing possible text "modes" for terminal output, useful to set the - text to bold, underline, blinking, etc... - Re-exported from d-colorize in dub.internal.colorize. See the enum definition - there for a list of possible values. -*/ -public alias Mode = mode; - -/// The tag width in chars, defined as a constant here -private const int TAG_WIDTH = 12; - -/// Possible log levels supported -enum LogLevel { - debug_, - diagnostic, - info, - warn, - error, - none -} - -// The current minimum log level to be printed -private shared LogLevel _minLevel = LogLevel.info; - -/* - Whether to print text with colors or not, defaults to true but will be set - to false in initLogging() if stdout or stderr are not a TTY (which means the - output is probably being piped and we don't want ASCII escape chars in it) -*/ -private shared bool _printColors = true; - -// isatty() is used in initLogging() to detect whether or not we are on a TTY -extern (C) int isatty(int); - -/** - This function must be called at the beginning for the program, before any - logging occurs. It will detect whether or not stdout/stderr are a console/TTY - and will consequently disable colored output if needed. - - Forgetting to call the function will result in ASCII escape sequences in the - piped output, probably an undesiderable thing. -*/ -void initLogging() -{ - import core.stdc.stdio; - - // Initially enable colors, we'll disable them during this functions if we - // find any reason to - _printColors = true; - - // The following stuff depends on the platform - version (Windows) - { - version (CRuntime_DigitalMars) - { - if (!isatty(core.stdc.stdio.stdout._file) || - !isatty(core.stdc.stdio.stderr._file)) - _printColors = false; - } - else version (CRuntime_Microsoft) - { - if (!isatty(fileno(core.stdc.stdio.stdout)) || - !isatty(fileno(core.stdc.stdio.stderr))) - _printColors = false; - } - else - _printColors = false; - } - else version (Posix) - { - import core.sys.posix.unistd; - - if (!isatty(STDERR_FILENO) || !isatty(STDOUT_FILENO)) - _printColors = false; - } -} - -/// Sets the minimum log level to be printed -void setLogLevel(LogLevel level) nothrow -{ - _minLevel = level; -} - -/// Gets the minimum log level to be printed -LogLevel getLogLevel() -{ - return _minLevel; -} - -/// Set whether to print colors or not -void setLoggingColorsEnabled(bool enabled) -{ - _printColors = enabled; -} - -/** - Shorthand function to log a message with debug/diagnostic level, no tag string - or tag color required (since there will be no tag). - - Params: - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logDebug(T...)(string fmt, lazy T args) nothrow -{ - log(LogLevel.debug_, false, "", Color.init, fmt, args); -} - -/// ditto -void logDiagnostic(T...)(string fmt, lazy T args) nothrow -{ - log(LogLevel.diagnostic, false, "", Color.init, fmt, args); -} - -/** - Shorthand function to log a message with info level, with custom tag string - and tag color. - - Params: - tag = The string the tag at the beginning of the line should contain - tagColor = The color the tag string should have - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logInfo(T...)(string tag, Color tagColor, string fmt, lazy T args) nothrow -{ - log(LogLevel.info, false, tag, tagColor, fmt, args); -} - -/** - Shorthand function to log a message with info level, this version prints an - empty tag automatically (which is different from not having a tag - in this - case there will be an identation of TAG_WIDTH chars on the left anyway). - - Params: - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logInfo(T...)(string fmt, lazy T args) nothrow if (!is(T[0] : Color)) -{ - log(LogLevel.info, false, "", Color.init, fmt, args); -} - -/** - Shorthand function to log a message with info level, this version doesn't - print a tag at all, it effectively just prints the given string. - - Params: - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logInfoNoTag(T...)(string fmt, lazy T args) nothrow if (!is(T[0] : Color)) -{ - log(LogLevel.info, true, "", Color.init, fmt, args); -} - -/** - Shorthand function to log a message with warning level, with custom tag string. - The tag color is fixed to yellow. - - Params: - tag = The string the tag at the beginning of the line should contain - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logWarnTag(T...)(string tag, string fmt, lazy T args) nothrow -{ - log(LogLevel.warn, false, tag, Color.yellow, fmt, args); -} - -/** - Shorthand function to log a message with warning level, using the default - tag "Warning". The tag color is also fixed to yellow. - - Params: - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logWarn(T...)(string fmt, lazy T args) nothrow -{ - log(LogLevel.warn, false, "Warning", Color.yellow, fmt, args); -} - -/** - Shorthand function to log a message with error level, with custom tag string. - The tag color is fixed to red. - - Params: - tag = The string the tag at the beginning of the line should contain - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logErrorTag(T...)(string tag, string fmt, lazy T args) nothrow -{ - log(LogLevel.error, false, tag, Color.red, fmt, args); -} - -/** - Shorthand function to log a message with error level, using the default - tag "Error". The tag color is also fixed to red. - - Params: - level = The log level for the logged message - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void logError(T...)(string fmt, lazy T args) nothrow -{ - log(LogLevel.error, false, "Error", Color.red, fmt, args); -} - -/** - Log a message with the specified log level and with the specified tag string - and color. If the log level is debug or diagnostic, the tag is not printed - thus the tag string and tag color will be ignored. If the log level is error - or warning, the tag will be in bold text. Also the tag can be disabled (for - any log level) by passing true as the second argument. - - Params: - level = The log level for the logged message - disableTag = Setting this to true disables the tag, no matter what - tag = The string the tag at the beginning of the line should contain - tagColor = The color the tag string should have - fmt = See http://dlang.org/phobos/std_format.html#format-string -*/ -void log(T...)( - LogLevel level, - bool disableTag, - string tag, - Color tagColor, - string fmt, - lazy T args -) nothrow -{ - if (level < _minLevel) - return; - - auto hasTag = true; - if (level <= LogLevel.diagnostic) - hasTag = false; - if (disableTag) - hasTag = false; - - auto boldTag = false; - if (level >= LogLevel.warn) - boldTag = true; - - try - { - string result = format(fmt, args); - - if (hasTag) - result = tag.rightJustify(TAG_WIDTH, ' ').color(tagColor, boldTag ? Mode.bold : Mode.init) ~ " " ~ result; - - import dub.internal.colorize : cwrite; - - File output = (level <= LogLevel.info) ? stdout : stderr; - - if (output.isOpen) - { - output.cwrite(result, "\n"); - output.flush(); - } - } - catch (Exception e) - { - debug assert(false, e.msg); - } -} - -/** - Colors the specified string with the specified color. The function is used to - print colored text within a log message. The function also checks whether - color output is enabled or disabled (when not outputting to a TTY) and, in the - last case, just returns the plain string. This allows to use it like so: - - logInfo("Tag", Color.green, "My %s log message", "colored".color(Color.red)); - - without worring whether or not colored output is enabled or not. - - Also a mode can be specified, such as bold/underline/etc... - - Params: - str = The string to color - color = The color to apply - mode = An optional mode, such as bold/underline/etc... -*/ -string color(const string str, const Color c, const Mode m = Mode.init) -{ - import dub.internal.colorize; - - if (_printColors) - return dub.internal.colorize.color(str, c, bg.init, m); - else - return str; -} - -/** - This function is the same as the above one, but just accepts a mode. - It's useful, for instance, when outputting bold text without changing the - color. - - Params: - str = The string to color - mode = The mode, such as bold/underline/etc... -*/ -string color(const string str, const Mode m = Mode.init) -{ - import dub.internal.colorize; - - if (_printColors) - return dub.internal.colorize.color(str, fg.init, bg.init, m); - else - return str; -} diff --git a/source/dub/package_.d b/source/dub/package_.d index 71d019a..d5daff7 100644 --- a/source/dub/package_.d +++ b/source/dub/package_.d @@ -12,10 +12,10 @@ import dub.compilers.compiler; import dub.dependency; import dub.description; -import dub.logging; import dub.recipe.json; import dub.recipe.sdl; +import dub.internal.logging; import dub.internal.utils; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; diff --git a/source/dub/packagemanager.d b/source/dub/packagemanager.d index afab9de..4e1464d 100644 --- a/source/dub/packagemanager.d +++ b/source/dub/packagemanager.d @@ -13,7 +13,7 @@ import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import dub.package_; import std.algorithm : countUntil, filter, sort, canFind, remove; diff --git a/source/dub/packagesuppliers/fallback.d b/source/dub/packagesuppliers/fallback.d index d7793e4..1103e37 100644 --- a/source/dub/packagesuppliers/fallback.d +++ b/source/dub/packagesuppliers/fallback.d @@ -46,7 +46,7 @@ { import std.format : format; enum fallback = q{ - import dub.logging : logDebug; + import dub.internal.logging : logDebug; Exception firstEx; try diff --git a/source/dub/packagesuppliers/filesystem.d b/source/dub/packagesuppliers/filesystem.d index 1e53f00..fcb9efd 100644 --- a/source/dub/packagesuppliers/filesystem.d +++ b/source/dub/packagesuppliers/filesystem.d @@ -9,7 +9,7 @@ the form "[package name]-[version].zip". */ class FileSystemPackageSupplier : PackageSupplier { - import dub.logging; + import dub.internal.logging; version (Have_vibe_core) import dub.internal.vibecompat.inet.path : toNativeString; import std.exception : enforce; diff --git a/source/dub/packagesuppliers/maven.d b/source/dub/packagesuppliers/maven.d index 8163819..4899844 100644 --- a/source/dub/packagesuppliers/maven.d +++ b/source/dub/packagesuppliers/maven.d @@ -12,7 +12,7 @@ import dub.internal.utils : retryDownload, HTTPStatusException; import dub.internal.vibecompat.data.json : serializeToJson; import dub.internal.vibecompat.inet.url : URL; - import dub.logging; + import dub.internal.logging; import std.datetime : Clock, Duration, hours, SysTime, UTC; diff --git a/source/dub/packagesuppliers/registry.d b/source/dub/packagesuppliers/registry.d index 2e132ad..a5afa64 100644 --- a/source/dub/packagesuppliers/registry.d +++ b/source/dub/packagesuppliers/registry.d @@ -14,7 +14,7 @@ import dub.internal.utils : download, retryDownload, HTTPStatusException; import dub.internal.vibecompat.data.json : parseJson, parseJsonString, serializeToJson; import dub.internal.vibecompat.inet.url : URL; - import dub.logging; + import dub.internal.logging; import std.uri : encodeComponent; import std.datetime : Clock, Duration, hours, SysTime, UTC; diff --git a/source/dub/project.d b/source/dub/project.d index 4b514a2..8d9e419 100644 --- a/source/dub/project.d +++ b/source/dub/project.d @@ -15,7 +15,7 @@ import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.data.json; import dub.internal.vibecompat.inet.path; -import dub.logging; +import dub.internal.logging; import dub.package_; import dub.packagemanager; import dub.recipe.selection; diff --git a/source/dub/recipe/packagerecipe.d b/source/dub/recipe/packagerecipe.d index 8509d12..9996409 100644 --- a/source/dub/recipe/packagerecipe.d +++ b/source/dub/recipe/packagerecipe.d @@ -10,7 +10,7 @@ import dub.compilers.compiler; import dub.compilers.utils : warnOnSpecialCompilerFlags; import dub.dependency; -import dub.logging; +import dub.internal.logging; import dub.internal.vibecompat.core.file; import dub.internal.vibecompat.inet.path; diff --git a/source/dub/recipe/sdl.d b/source/dub/recipe/sdl.d index 779beb8..b7de2d2 100644 --- a/source/dub/recipe/sdl.d +++ b/source/dub/recipe/sdl.d @@ -9,7 +9,7 @@ import dub.compilers.compiler; import dub.dependency; -import dub.logging; +import dub.internal.logging; import dub.internal.sdlang; import dub.internal.vibecompat.inet.path; import dub.recipe.packagerecipe;