blob: 714d03a707f2e26da01eb174f63c1ee76dae3155 [file] [log] [blame]
Matteo Scandolo2bf742a2019-10-01 11:33:34 -07001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo2bf742a2019-10-01 11:33:34 -07003
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package commands
18
19import (
20 "context"
21 "fmt"
Shrey Baid688b4242020-07-10 20:40:10 +053022
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070023 "github.com/jessevdk/go-flags"
24 pb "github.com/opencord/bbsim/api/bbsim"
25 "github.com/opencord/bbsim/internal/bbsimctl/config"
26 log "github.com/sirupsen/logrus"
27 "google.golang.org/grpc"
28)
29
30type LoggingOptions struct {
31 Args struct {
32 Level string
33 Caller bool
34 } `positional-args:"yes" required:"yes"`
35}
36
37func RegisterLoggingCommands(parent *flags.Parser) {
Shrey Baid688b4242020-07-10 20:40:10 +053038 _, _ = parent.AddCommand("log", "set bbsim log level", "Commands to set the log level", &LoggingOptions{})
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070039}
40
41func (options *LoggingOptions) Execute(args []string) error {
42 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
43
44 if err != nil {
45 log.Fatalf("did not connect: %v", err)
46 return nil
47 }
48 defer conn.Close()
49 c := pb.NewBBSimClient(conn)
50
51 // Contact the server and print out its response.
52
53 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
54 defer cancel()
55
56 req := pb.LogLevel{
57 Level: options.Args.Level,
58 Caller: options.Args.Caller,
59 }
60
61 logLevel, err := c.SetLogLevel(ctx, &req)
62
Shrey Baid688b4242020-07-10 20:40:10 +053063 if err != nil {
64 log.Fatalf("could not set log level: %v", err)
65 }
66
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070067 fmt.Println("New log settings:")
68 fmt.Println(fmt.Sprintf("\tLevel: %s", logLevel.Level))
69 fmt.Println(fmt.Sprintf("\tReportCaller: %t", logLevel.Caller))
70
71 return nil
72}