Skip to content

Commit

Permalink
Generate mod log names
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kasjer committed Apr 13, 2024
1 parent a798fcc commit cb6f87f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion newt/builder/targetbuild.go
Expand Up @@ -304,7 +304,7 @@ func (t *TargetBuilder) validateAndWriteCfg() error {
return err
}

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

Expand Down
84 changes: 71 additions & 13 deletions newt/logcfg/logcfg.go
Expand Up @@ -53,6 +53,9 @@ type Log struct {

// The level assigned to this log.
Level val.ValSetting

// The log's string name
NameStr string
}

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

nameStr := logMap["name"]
if nameStr == "" {
// If there is no "name:" node, use log id without _LOG suffix
nameStr = "\"" + strings.Replace(name, "_LOG", "", 1) + "\""
}

cl.Module = mod
cl.Level = level
cl.NameStr = nameStr

return cl, nil
}
Expand Down Expand Up @@ -284,8 +294,28 @@ func (lcfg *LCfg) writeLogMacros(w io.Writer) {
}
}

// Write log C macro definitions for each log in the log configuration.
func (lcfg *LCfg) writeLogModuleNames(w io.Writer) {
logs := lcfg.sortedLogs()
fmt.Fprintf(w, "\n")
for _, l := range logs {
fmt.Fprintf(w,
"#define %s_NAME %s\n",
l.Name, l.NameStr)
}
}

func (lcfg *LCfg) writeLogModuleCases(w io.Writer) {
logs := lcfg.sortedLogs()
for _, l := range logs {
fmt.Fprintf(w,
" case %s: return %s_NAME;\n",
l.Module.Value, l.Name)
}
}

// Writes a logcfg header file to the specified writer.
func (lcfg *LCfg) write(w io.Writer) {
func (lcfg *LCfg) writeHeader(w io.Writer) {
fmt.Fprintf(w, newtutil.GeneratedPreamble())

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

lcfg.writeLogMacros(w)
lcfg.writeLogModuleNames(w)
fmt.Fprintf(w, "\n")
}
fmt.Fprintf(w, "const char *logcfg_log_module_name(uint8_t id);\n\n")

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

// Writes a logcfg src file to the specified writer.
func (lcfg *LCfg) writeSource(w io.Writer) {
fmt.Fprintf(w, newtutil.GeneratedPreamble())

fmt.Fprintf(w, "#include <stdint.h>\n")
fmt.Fprintf(w, "#include <stdlib.h>\n")
fmt.Fprintf(w, "#include <logcfg/logcfg.h>\n\n")
fmt.Fprintf(w, "const char *\nlogcfg_log_module_name(uint8_t id)\n{\n")
fmt.Fprintf(w, " switch (id) {\n")
lcfg.writeLogModuleCases(w)
fmt.Fprintf(w, " default: return NULL;\n }\n}\n")
}

// Ensures an up-to-date logcfg header is written for the target.
func (lcfg *LCfg) EnsureWritten(includeDir string) error {
func (lcfg *LCfg) EnsureWritten(includeDir string, srcDir string, targetName string) error {
buf := bytes.Buffer{}
lcfg.write(&buf)
srcBuf := bytes.Buffer{}
lcfg.writeHeader(&buf)
lcfg.writeSource(&srcBuf)

path := includeDir + "/" + HEADER_PATH

writeReqd, err := util.FileContentsChanged(path, buf.Bytes())
if err != nil {
return err
}
if !writeReqd {
log.Debugf("logcfg unchanged; not writing header file (%s).", path)
return nil
}
if writeReqd {
log.Debugf("logcfg changed; writing header file (%s).", path)

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

if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return util.NewNewtError(err.Error())
if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil {
return util.NewNewtError(err.Error())
}
} else {
log.Debugf("logcfg unchanged; not writing header file (%s).", path)
}

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

if writeReqd {
log.Debugf("logcfg changed; writing source file (%s).", path)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return util.NewNewtError(err.Error())
}
if err := os.WriteFile(path, srcBuf.Bytes(), 0644); err != nil {
return util.NewNewtError(err.Error())
}
}
return nil
}

0 comments on commit cb6f87f

Please sign in to comment.