log: add log.use_stdout(), use it to silence the transition note for the most commonly used V tools/examples (#23642)

This commit is contained in:
Delyan Angelov 2025-02-03 12:37:57 +02:00 committed by GitHub
parent 23c3af8b4d
commit 319eb83525
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 24 additions and 7 deletions

View File

@ -34,6 +34,7 @@ pub fn (t Task) run() {
pub fn run(all_tasks map[string]Task) {
unbuffer_stdout()
log.use_stdout()
if os.args.len < 2 {
println('Usage: v run macos_ci.vsh <task_name>')
println('Available tasks are: ${all_tasks.keys()}')

View File

@ -41,6 +41,7 @@ fn lexec(cmd string) string {
fn main() {
// ensure all log messages will be visible to the observers, even if the program panics
log.use_stdout()
log.set_always_flush(true)
total_sw := time.new_stopwatch()

View File

@ -97,6 +97,7 @@ struct FlagOptions {
}
fn main() {
log.use_stdout()
mut fp := flag.new_flag_parser(os.args.clone())
fp.application(app_name)
fp.version(app_version)

View File

@ -148,6 +148,7 @@ fn normalize_path(path string) string {
}
fn main() {
log.use_stdout()
mut ctx := Context{}
ctx.working_folder = normalize_path(os.real_path(os.getwd()))
mut fp := flag.new_flag_parser(os.args#[1..])

View File

@ -23,9 +23,7 @@ mut:
const vexe = os.real_path(os.getenv_opt('VEXE') or { @VEXE })
fn main() {
mut l := log.ThreadSafeLog{}
l.set_output_stream(os.stdout())
log.set_logger(l)
log.use_stdout()
mut ctx := Context{}
mut fp := flag.new_flag_parser(os.args#[1..])
fp.application(os.file_name(os.executable()))

View File

@ -22,6 +22,7 @@ fn run(cmd string) os.Result {
}
fn test_retry() {
log.use_stdout()
log.warn('start...')
defer {
log.warn('... done')

View File

@ -4,6 +4,7 @@ import log
const should_clean = os.args.contains('-c')
fn main() {
log.use_stdout()
mut files := []string{}
args := os.args#[2..].filter(it != '-c')
for a in args {

View File

@ -8,6 +8,7 @@ import sim.args as simargs
fn main() {
unbuffer_stdout()
log.use_stdout()
args := simargs.parse_args(extra_workers: 1)! as simargs.ParallelArgs
mut app := anim.new_app(args)
mut workers := []thread{cap: args.workers}

View File

@ -7,6 +7,7 @@ import net.http
const url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
fn main() {
log.use_stdout()
mut old_rate := f64(0)
for i := u64(1); true; i++ {
data := http.get(url) or {

View File

@ -12,6 +12,7 @@ import net.websocket
const app_port = 8990
fn main() {
log.use_stdout()
mut app := &App{
wss: new_websocker_server()!
}
@ -64,6 +65,7 @@ fn wlog(message string) {
fn new_websocker_server() !&websocket.Server {
mut logger := &log.Log{}
logger.set_level(.info)
logger.set_output_stream(os.stderr())
mut wss := websocket.new_server(.ip, app_port, '', logger: logger)
wss.set_ping_interval(100)
wss.on_connect(fn [mut logger] (mut server_client websocket.ServerClient) !bool {

View File

@ -58,16 +58,17 @@ fn main() {
After 2025/01/21, the `log` module outputs to `stderr` by default.
Before that, it used `stdout` by default.
If you want to restore the previous behaviour, you have to explicitly call l.set_output_stream():
If you want to restore the previous behaviour, you have to explicitly call `log.use_stdout()` :
```v
import os
import log
fn main() {
// log.info('this will be printed to stderr after 2025/01/21 by default')
mut l := log.ThreadSafeLog{}
l.set_output_stream(os.stdout())
log.set_logger(l)
log.use_stdout()
log.info('this will be printed to stdout')
}
```
If you want to just silence the note about the stdout -> stderr, during the transition period,
call `l.set_output_stream(os.stderr())` explicitly.

View File

@ -324,3 +324,11 @@ pub fn (mut l Log) set_short_tag(enabled bool) {
pub fn (l Log) get_short_tag() bool {
return l.short_tag
}
// use_stdout will restore the old behaviour of logging to stdout, instead of stderr.
// It will also silence the deprecation note in the transition period.
pub fn use_stdout() {
mut l := ThreadSafeLog{}
l.set_output_stream(os.stdout())
set_logger(l)
}