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

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

Merged
merged 1 commit into from
Feb 3, 2025
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
1 change: 1 addition & 0 deletions ci/common/runner.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()}')
Expand Down
1 change: 1 addition & 0 deletions cmd/tools/fast/fast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions cmd/tools/gen_vc.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/tools/vcover/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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..])
Expand Down
4 changes: 1 addition & 3 deletions cmd/tools/vdownload.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
1 change: 1 addition & 0 deletions cmd/tools/vretry_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn run(cmd string) os.Result {
}

fn test_retry() {
log.use_stdout()
log.warn('start...')
defer {
log.warn('... done')
Expand Down
1 change: 1 addition & 0 deletions cmd/tools/vshould-compile-all.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions examples/pendulum-simulation/animation.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions examples/poll_coindesk_bitcoin_vs_usd_rate.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions examples/veb/websocket/server.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.websocket
const app_port = 8990

fn main() {
log.use_stdout()
mut app := &App{
wss: new_websocker_server()!
}
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions vlib/log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 8 additions & 0 deletions vlib/log/log.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading