Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 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 | |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 22 | |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 23 | "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 | |
| 30 | type LoggingOptions struct { |
| 31 | Args struct { |
| 32 | Level string |
| 33 | Caller bool |
| 34 | } `positional-args:"yes" required:"yes"` |
| 35 | } |
| 36 | |
| 37 | func RegisterLoggingCommands(parent *flags.Parser) { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 38 | _, _ = parent.AddCommand("log", "set bbsim log level", "Commands to set the log level", &LoggingOptions{}) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | func (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 Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 63 | if err != nil { |
| 64 | log.Fatalf("could not set log level: %v", err) |
| 65 | } |
| 66 | |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 67 | 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 | } |