Skip to content

Commit

Permalink
feat(plc4go): introduce Plc4xModelLog
Browse files Browse the repository at this point in the history
+ This feature allows to set different log levels for each protocol by overriding the logger
  • Loading branch information
sruehl committed Aug 30, 2022
1 parent 9500fef commit 13809af
Show file tree
Hide file tree
Showing 463 changed files with 1,076 additions and 1,142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -55,11 +56,13 @@ public void generate(File outputDir, String version, String languageName, String
List<Template> complexTypesTemplateList;
List<Template> enumTypesTemplateList;
List<Template> dataIoTemplateList;
List<Template> miscTemplateList;
try {
specTemplates = getSpecTemplates(freemarkerConfiguration);
complexTypesTemplateList = getComplexTypeTemplates(freemarkerConfiguration);
enumTypesTemplateList = getEnumTypeTemplates(freemarkerConfiguration);
dataIoTemplateList = getDataIoTemplates(freemarkerConfiguration);
miscTemplateList = getMiscTemplates(freemarkerConfiguration);
} catch (IOException e) {
throw new GenerationException("Error getting template", e);
}
Expand Down Expand Up @@ -117,6 +120,24 @@ public void generate(File outputDir, String version, String languageName, String
}
}
}

// Generate misc outputs
if (!miscTemplateList.isEmpty()) {
Map<String, Object> typeContext = new HashMap<>();
typeContext.put("languageName", languageName);
typeContext.put("protocolName", protocolName);
typeContext.put("outputFlavor", outputFlavor);
typeContext.put("helper", getHelper(null, protocolName, outputFlavor, types, options));
typeContext.putAll(options);

for (Template template : miscTemplateList) {
try {
renderTemplate(outputDir, template, typeContext);
} catch (IOException | TemplateException e) {
throw new GenerationException("Error generating misc protocol output.", e);
}
}
}
}

protected void renderTemplate(File outputDir, Template template, Map<String, Object> context)
Expand Down Expand Up @@ -186,6 +207,10 @@ private Configuration getFreemarkerConfiguration() throws GenerationException {

protected abstract List<Template> getDataIoTemplates(Configuration freemarkerConfiguration) throws IOException;

protected List<Template> getMiscTemplates(Configuration freemarkerConfiguration) throws IOException {
return Collections.emptyList();
}

protected abstract FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ protected List<Template> getDataIoTemplates(Configuration freemarkerConfiguratio
freemarkerConfiguration.getTemplate("templates/go/data-io-template.go.ftlh"));
}

protected List<Template> getMiscTemplates(Configuration freemarkerConfiguration) throws IOException {
return Collections.singletonList(
freemarkerConfiguration.getTemplate("templates/go/plc4x_common.go.ftlh"));
}

@Override
protected FreemarkerLanguageTemplateHelper getHelper(TypeDefinition thisType, String protocolName, String flavorName, Map<String, TypeDefinition> types,
Map<String, String> options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer<#if hasParserArguments>, ${pa

switch {
case errors.Is(_err, utils.ParseAssertError{}) || errors.Is(_err, io.EOF):<@emitImport import="io" />
log.Debug().Err(_err).Msg("Resetting position because optional threw an error")<@emitImport import="github.com/rs/zerolog/log" />
Plc4xModelLog.Debug().Err(_err).Msg("Resetting position because optional threw an error")
readBuffer.Reset(currentPos)
case _err != nil:
return nil, errors.Wrap(_err, "Error parsing '${optionalField.name}' field of ${type.name}")<@emitImport import="github.com/pkg/errors" />
Expand Down Expand Up @@ -1288,7 +1288,7 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer<#if hasParserArguments>, ${pa
return nil, errors.Wrap(_err, "Error parsing 'reserved' field of ${type.name}")<@emitImport import="github.com/pkg/errors" />
}
if ${helper.toTypeSafeCompare(reservedField)} {
log.Info().Fields(map[string]interface{}{<@emitImport import="github.com/rs/zerolog/log" />
Plc4xModelLog.Info().Fields(map[string]interface{}{
"expected value": ${helper.getReservedValue(reservedField)},
"got value": reserved,
}).Msg("Got unexpected response for reserved field.")
Expand Down Expand Up @@ -1815,7 +1815,7 @@ func (m *_${type.name}) Serialize(writeBuffer utils.WriteBuffer) error {
{
var reserved ${helper.getLanguageTypeNameForTypeReference(simpleTypeReference)} = ${helper.getReservedValue(reservedField)}
if <#if type.isAbstract()>p</#if>m.reservedField${reservedFieldIndex} != nil {
log.Info().Fields(map[string]interface{}{<@emitImport import="github.com/rs/zerolog/log" />
Plc4xModelLog.Info().Fields(map[string]interface{}{
"expected value": ${helper.getReservedValue(reservedField)},
"got value": reserved,
}).Msg("Overriding reserved field with unexpected value.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ package model
import (
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

// Code generated by code-generation. DO NOT EDIT.
Expand Down Expand Up @@ -184,7 +183,7 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer) (${type.name}, error) {
return <#if type.type.orElseThrow().isStringTypeReference() || type.type.orElseThrow().isVstringTypeReference()>""<#elseif baseType == "bool">false<#else>0</#if>, errors.Wrap(err, "error reading ${type.name}")
}
if enum, ok := ${type.name}ByValue(val); !ok {
log.Debug().Msgf("no value %x found for RequestType", val)
Plc4xModelLog.Debug().Msgf("no value %x found for RequestType", val)
return ${type.name}(val), nil
} else {
return enum, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<#--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<#-- Prevent freemarker from escaping stuff -->
<#outputformat "undefined">
<#-- Declare the name and type of variables passed in to the template -->
<#-- @ftlvariable name="languageName" type="java.lang.String" -->
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.go.GoLanguageTemplateHelper" -->
${helper.fileName(protocolName, languageName, outputFlavor)?replace(".", "/")}/model/plc4x_common.go
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package model

import "github.com/rs/zerolog/log"

// Code generated by code-generation. DO NOT EDIT.

// Plc4xModelLog is the Logger used by the Parse/Serialize methods
var Plc4xModelLog = log.Logger

</#outputformat>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions plc4go/protocols/abeth/readwrite/model/DF1RequestMessage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions plc4go/protocols/abeth/readwrite/model/DF1ResponseMessage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions plc4go/protocols/abeth/readwrite/model/plc4x_common.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions plc4go/protocols/ads/readwrite/model/AdsDataType.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions plc4go/protocols/ads/readwrite/model/AdsSymbolTableEntry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 13809af

Please sign in to comment.