From e8ae139371e7488dbcc0f2af62f09ea9ec49427b Mon Sep 17 00:00:00 2001 From: Scantlight Date: Wed, 30 Jan 2013 14:15:39 +0100 Subject: [PATCH 1/3] flag parse + log module + configuration file flag parse, application can be started using some basic command line arguments: log - specify amount of output log (text messages) conf - path to a configuration file configuration file, function to load and parse configuration file log module, when log level is to hight application will panic --- admin/admin.go | 23 +++++++++++++++++++++++ log/.log.go.swo | Bin 12288 -> 0 bytes log/log.go | 8 +++++++- mapo.go | 25 +++++++++++++++++++++---- 4 files changed, 51 insertions(+), 5 deletions(-) delete mode 100644 log/.log.go.swo diff --git a/admin/admin.go b/admin/admin.go index feda89c..561f98d 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -21,3 +21,26 @@ along with Mapo. If not, see . Package admin implements the API for Mapo's administration components. */ package admin + +import ( + "gconf/conf" +) + +/* +GlobalConfiguration, il oggetto globale per l'accesso ai dati contenuti nel +file di configurazione. +*/ +var GlobalConfiguration *conf.ConfigFile + +/* +ReadConfiguration, attiva il GlobalConfiguration. +*/ +func ReadConfiguration(filepath string) error { + + c, err := conf.ReadConfigFile(filepath) + if err == nil { + GlobalConfiguration = c + } + + return err +} diff --git a/log/.log.go.swo b/log/.log.go.swo deleted file mode 100644 index 31bbc0735276017e5aef0f3d0fa4d67ce4a3b674..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&ubiI7{{M_u&i1uN{ikeS1Oyboo$jsC2f>#nzW1CWV_k5#H#S_>^r-Iv-2+V zzOx~%(X&dyqo4@>0cvTH)`Lec7EuuN;?YAv@us(c-*;xWk-FC6t?~vwnVol@_xbUC zo|zsO?F5%rZeUiPW01|p27 z?09qRu+o*I%0LWN_AQpCq7y$~h*YlvyxzY@of5z;IR%^o(-lZfaOAmtG(R_c)EYcf ze43wlygl94tvUsq0!{&^fK$LJ;1qBQI0c*n|4RkTa1XtO${rY3cW!*#bH{Of>JCl; zr+`zyDc}@v3OEIv0!{&^fK$LJ;1qBQ+=mK?F46CMiP{AukN^Kq{{G+DPjmxZ2OokD zz-!kI?z zlSv?ad1-rwFHxnE+cI8@)GT-)wH7_e!f-KjU*t%RWQ1RdCQ)mvD{E7O*5W|jV@zv0 zEu{7R_lV2Q=2~-V$mJwa$vwu*Y^Mdy-2XM|Hc_Dv42;*XG6$vNAXVLWD^dgx2iTvGu}$plv1|RTv?i(otw92*k%o_^YyHW!8rR# z`(Oj5Gehg27bktdqA#@MtSbw5m}I(5EbPgh26>GWi|ovr=*`gh{k+AP2CR%kGO2Am zI8+FOvBil+j=XFhV4MwFK8X1ew3;;Ot}|yxY2pSBv+>HDtxHq4XQkWz#v?kpz*-v3 zrY~8gW|*3BTA1@}u@|Y1h-`~{G9j|1?4H@6);S|hGFIB$&Ze6B##WC8W$IGFN=SuF zCUhWFMv$*!ZMbCbnCeJ`k?2I3i#!LN?JnAO7pYK0e~T0jhAOdJXej*G?Olb4bhtu| z-BX5JgZG*3y{EAOJ_l)J!XtUEd5RuY4wKy|A{F--J?ryXyx{CH*8+|eWY<#iiM}zz zg-T^}bJOd^sizXW?Hu|t)>2oV_h>_qoQ?Gf(}y$?LA*>omp$QxVpN*=c~;ooNXM>>PP<> z-Hfx0t!^Re*c3@3Vq+^Is}F~oyP=)iOwex9?3*w$s;>~D+v-^SAjuPoT)VK3cI1q68+h==DCTmV-?E~ zf)E)SU}asw_drjq?jo_!JhTnbeH7E#x?N*nQ_kGlrCalYF}|f^rBFd Uxt_nv&y5g2TqNoW{m>5WpVYT|z5oCK diff --git a/log/log.go b/log/log.go index b39a22f..52a0985 100644 --- a/log/log.go +++ b/log/log.go @@ -42,7 +42,13 @@ var l logger // SetLevel sets the output level for the global logger func SetLevel(level int) { - l.level = level + + if level <= DEBUG { + l.level = level + return + } + + panic(fmt.Sprintf("Unknown log level %v", level)) } func print(level int, format string, v ...interface{}) { diff --git a/mapo.go b/mapo.go index 5c2790b..2cc952c 100644 --- a/mapo.go +++ b/mapo.go @@ -21,16 +21,33 @@ package main import ( "mapo/log" + "mapo/admin" + + "flag" ) func main() { + + log.Info("Starting application") + // parse flags + var logLevel = flag.Int("log", 1, "set message level eg: 0 = DEBUG, 1 = INFO, 2 = ERROR") + var confFilePath = flag.String("conf", "./conf.ini", "set path to configuration file") + flag.Parse() - // load config and setup application - log.SetLevel(log.DEBUG) - log.Info("Setting log level to DEBUG") + // set log level + log.SetLevel(*logLevel) + log.Info("Setting log level to %d", *logLevel) - log.Info("Starting application") + // load config and setup application + log.Info("Loading configuration from file") + err := admin.ReadConfiguration(*confFilePath) + if err != nil { + log.Info("%s, no such file or directory", *confFilePath) + return + } + + // setup application // register with supervisor log.Info("Joining supervisor") From 3f066445e3ce73ef78455a4e9c16f2ec3b2d03c7 Mon Sep 17 00:00:00 2001 From: Lorenzo Pierfederici Date: Wed, 30 Jan 2013 21:57:17 -0800 Subject: [PATCH 2/3] reformat code with gofmt --- admin/admin.go | 12 ++++++------ log/log.go | 10 +++++----- mapo.go | 38 ++++++++++++++++++-------------------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/admin/admin.go b/admin/admin.go index 561f98d..de01598 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -23,7 +23,7 @@ Package admin implements the API for Mapo's administration components. package admin import ( - "gconf/conf" + "gconf/conf" ) /* @@ -37,10 +37,10 @@ ReadConfiguration, attiva il GlobalConfiguration. */ func ReadConfiguration(filepath string) error { - c, err := conf.ReadConfigFile(filepath) - if err == nil { - GlobalConfiguration = c - } + c, err := conf.ReadConfigFile(filepath) + if err == nil { + GlobalConfiguration = c + } - return err + return err } diff --git a/log/log.go b/log/log.go index 52a0985..c196ed0 100644 --- a/log/log.go +++ b/log/log.go @@ -43,12 +43,12 @@ var l logger // SetLevel sets the output level for the global logger func SetLevel(level int) { - if level <= DEBUG { - l.level = level - return - } + if level <= DEBUG { + l.level = level + return + } - panic(fmt.Sprintf("Unknown log level %v", level)) + panic(fmt.Sprintf("Unknown log level %v", level)) } func print(level int, format string, v ...interface{}) { diff --git a/mapo.go b/mapo.go index 2cc952c..9d1625c 100644 --- a/mapo.go +++ b/mapo.go @@ -20,32 +20,31 @@ along with Mapo. If not, see . package main import ( + "mapo/admin" "mapo/log" - "mapo/admin" - "flag" + "flag" ) func main() { - log.Info("Starting application") // parse flags - var logLevel = flag.Int("log", 1, "set message level eg: 0 = DEBUG, 1 = INFO, 2 = ERROR") - var confFilePath = flag.String("conf", "./conf.ini", "set path to configuration file") - flag.Parse() + var logLevel = flag.Int("log", 1, "set message level eg: 0 = DEBUG, 1 = INFO, 2 = ERROR") + var confFilePath = flag.String("conf", "./conf.ini", "set path to configuration file") + flag.Parse() - // set log level + // set log level log.SetLevel(*logLevel) log.Info("Setting log level to %d", *logLevel) // load config and setup application log.Info("Loading configuration from file") - err := admin.ReadConfiguration(*confFilePath) - if err != nil { - log.Info("%s, no such file or directory", *confFilePath) - return - } + err := admin.ReadConfiguration(*confFilePath) + if err != nil { + log.Info("%s, no such file or directory", *confFilePath) + return + } // setup application @@ -67,20 +66,19 @@ func main() { // inform supervisor that we are up // for each request - // check authentication/authorization - - // extract request operation + // check authentication/authorization - // extract request arguments + // extract request operation - // pass operation and arguments to api.router + // extract request arguments - // find function mapped to operation + // pass operation and arguments to api.router - // call function with arguments + // find function mapped to operation - // return result to user + // call function with arguments + // return result to user // close on signal log.Info("Closing application") From 56fb393cccb636a07549eac95a694b8383f99aaa Mon Sep 17 00:00:00 2001 From: Lorenzo Pierfederici Date: Sun, 3 Mar 2013 22:46:25 -0800 Subject: [PATCH 3/3] use "log" and "conf" from maponet/utils --- mapo.go | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/mapo.go b/mapo.go index 9d1625c..da1d4cd 100644 --- a/mapo.go +++ b/mapo.go @@ -20,31 +20,45 @@ along with Mapo. If not, see . package main import ( - "mapo/admin" - "mapo/log" + "github.com/maponet/utils/log" + "github.com/maponet/utils/conf" "flag" ) func main() { - log.Info("Starting application") + var err error + + // set config defaults + conf.Set("logLevel", "INFO") // parse flags - var logLevel = flag.Int("log", 1, "set message level eg: 0 = DEBUG, 1 = INFO, 2 = ERROR") - var confFilePath = flag.String("conf", "./conf.ini", "set path to configuration file") + var flagLogLevel, flagConfPath string + flag.StringVar(&flagConfPath, "conf", "/etc/mapo/mapo.conf", "set path to configuration file") + flag.StringVar(&flagLogLevel, "log", "", "set loglevel [ERROR|INFO|DEBUG]") flag.Parse() - // set log level - log.SetLevel(*logLevel) - log.Info("Setting log level to %d", *logLevel) + // load config from file + confErr := conf.ParseFile(flagConfPath) + + // override config settings with command line flags + if flagLogLevel != "" { + conf.Set("logLevel", flagLogLevel) + } - // load config and setup application - log.Info("Loading configuration from file") - err := admin.ReadConfiguration(*confFilePath) + logLevel, _ := conf.GetString("logLevel") + err = log.SetLevel(logLevel) if err != nil { - log.Info("%s, no such file or directory", *confFilePath) - return + log.Fatal(err.Error()) + } + + log.Info("Starting application") + if confErr == nil { + log.Info("Loaded configuration file: %s", flagConfPath) + } else { + log.Error("Can't load config file (%s), using defaults", confErr.Error()) } + log.Info("Setting log level to: %s", logLevel) // setup application