Skip to content

Commit cb6f87f

Browse files
committed
Generate mod log names
This adds "name:" to syscfg.logs nodes. This is optional log name. With this change newt tool also generates file: <bsp>-logcfg.c that contains function: const char *logcfg_log_module_name(uint8_t id) that can be used to get module name for generated mod logs. If name is not present like in this example: syscfg.logs: BMA400_LOG: module: MYNEWT_VAL(BMA400_LOG_MODULE) level: MYNEWT_VAL(BMA400_LOG_LVL) module name will be taken from BMA400_LOG by cutting off trailing _LOG to make it BMA400
1 parent a798fcc commit cb6f87f

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed

newt/builder/targetbuild.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func (t *TargetBuilder) validateAndWriteCfg() error {
304304
return err
305305
}
306306

307-
if err := t.res.LCfg.EnsureWritten(incDir); err != nil {
307+
if err := t.res.LCfg.EnsureWritten(incDir, srcDir, pkg.ShortName(t.target.Package())); err != nil {
308308
return err
309309
}
310310

newt/logcfg/logcfg.go

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type Log struct {
5353

5454
// The level assigned to this log.
5555
Level val.ValSetting
56+
57+
// The log's string name
58+
NameStr string
5659
}
5760

5861
// Map of: [log-name] => log
@@ -147,8 +150,15 @@ func parseOneLog(name string, lpkg *pkg.LocalPackage, logMapItf interface{},
147150
"\"%s\" contains invalid \"level\": %s", name, err.Error())
148151
}
149152

153+
nameStr := logMap["name"]
154+
if nameStr == "" {
155+
// If there is no "name:" node, use log id without _LOG suffix
156+
nameStr = "\"" + strings.Replace(name, "_LOG", "", 1) + "\""
157+
}
158+
150159
cl.Module = mod
151160
cl.Level = level
161+
cl.NameStr = nameStr
152162

153163
return cl, nil
154164
}
@@ -284,8 +294,28 @@ func (lcfg *LCfg) writeLogMacros(w io.Writer) {
284294
}
285295
}
286296

297+
// Write log C macro definitions for each log in the log configuration.
298+
func (lcfg *LCfg) writeLogModuleNames(w io.Writer) {
299+
logs := lcfg.sortedLogs()
300+
fmt.Fprintf(w, "\n")
301+
for _, l := range logs {
302+
fmt.Fprintf(w,
303+
"#define %s_NAME %s\n",
304+
l.Name, l.NameStr)
305+
}
306+
}
307+
308+
func (lcfg *LCfg) writeLogModuleCases(w io.Writer) {
309+
logs := lcfg.sortedLogs()
310+
for _, l := range logs {
311+
fmt.Fprintf(w,
312+
" case %s: return %s_NAME;\n",
313+
l.Module.Value, l.Name)
314+
}
315+
}
316+
287317
// Writes a logcfg header file to the specified writer.
288-
func (lcfg *LCfg) write(w io.Writer) {
318+
func (lcfg *LCfg) writeHeader(w io.Writer) {
289319
fmt.Fprintf(w, newtutil.GeneratedPreamble())
290320

291321
fmt.Fprintf(w, "#ifndef H_MYNEWT_LOGCFG_\n")
@@ -296,37 +326,65 @@ func (lcfg *LCfg) write(w io.Writer) {
296326
fmt.Fprintf(w, "#include \"log_common/log_common.h\"\n")
297327

298328
lcfg.writeLogMacros(w)
329+
lcfg.writeLogModuleNames(w)
299330
fmt.Fprintf(w, "\n")
300331
}
332+
fmt.Fprintf(w, "const char *logcfg_log_module_name(uint8_t id);\n\n")
301333

302334
fmt.Fprintf(w, "#endif\n")
303335
}
304336

337+
// Writes a logcfg src file to the specified writer.
338+
func (lcfg *LCfg) writeSource(w io.Writer) {
339+
fmt.Fprintf(w, newtutil.GeneratedPreamble())
340+
341+
fmt.Fprintf(w, "#include <stdint.h>\n")
342+
fmt.Fprintf(w, "#include <stdlib.h>\n")
343+
fmt.Fprintf(w, "#include <logcfg/logcfg.h>\n\n")
344+
fmt.Fprintf(w, "const char *\nlogcfg_log_module_name(uint8_t id)\n{\n")
345+
fmt.Fprintf(w, " switch (id) {\n")
346+
lcfg.writeLogModuleCases(w)
347+
fmt.Fprintf(w, " default: return NULL;\n }\n}\n")
348+
}
349+
305350
// Ensures an up-to-date logcfg header is written for the target.
306-
func (lcfg *LCfg) EnsureWritten(includeDir string) error {
351+
func (lcfg *LCfg) EnsureWritten(includeDir string, srcDir string, targetName string) error {
307352
buf := bytes.Buffer{}
308-
lcfg.write(&buf)
353+
srcBuf := bytes.Buffer{}
354+
lcfg.writeHeader(&buf)
355+
lcfg.writeSource(&srcBuf)
309356

310357
path := includeDir + "/" + HEADER_PATH
311358

312359
writeReqd, err := util.FileContentsChanged(path, buf.Bytes())
313360
if err != nil {
314361
return err
315362
}
316-
if !writeReqd {
317-
log.Debugf("logcfg unchanged; not writing header file (%s).", path)
318-
return nil
319-
}
363+
if writeReqd {
364+
log.Debugf("logcfg changed; writing header file (%s).", path)
320365

321-
log.Debugf("logcfg changed; writing header file (%s).", path)
366+
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
367+
return util.NewNewtError(err.Error())
368+
}
322369

323-
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
324-
return util.NewNewtError(err.Error())
370+
if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil {
371+
return util.NewNewtError(err.Error())
372+
}
373+
} else {
374+
log.Debugf("logcfg unchanged; not writing header file (%s).", path)
325375
}
326376

327-
if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil {
328-
return util.NewNewtError(err.Error())
329-
}
377+
path = fmt.Sprintf("%s/%s-logcfg.c", srcDir, targetName)
378+
writeReqd, err = util.FileContentsChanged(path, srcBuf.Bytes())
330379

380+
if writeReqd {
381+
log.Debugf("logcfg changed; writing source file (%s).", path)
382+
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
383+
return util.NewNewtError(err.Error())
384+
}
385+
if err := os.WriteFile(path, srcBuf.Bytes(), 0644); err != nil {
386+
return util.NewNewtError(err.Error())
387+
}
388+
}
331389
return nil
332390
}

0 commit comments

Comments
 (0)