Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate mod log names #551

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion newt/builder/targetbuild.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
Loading