Skip to content

Commit

Permalink
Support Swift 3.1 (SwiftMetricsDash not currently working) (#96)
Browse files Browse the repository at this point in the history
* Fork agent core to support Swift 3.1

* rename header

* rename header

* move cpu header to include directory

* move cpu header to include directory

* update dependencies for 3.1

* update dependencies for 3.1

* Move cpuplugin.h

* Move header files

* Move header files

* moved headers

* moved headers

* undo change for testing

* install directory has changed

* remove whitespace changes

* Separate package.swift for 3.1

* Handle different install directories on 3.1/3.0 (#94)

* install directory has changed

* remove whitespace changes

* Minor change to paths
  • Loading branch information
sjanuary authored and mattcolegate committed Apr 12, 2017
1 parent d01ffd0 commit 299065a
Show file tree
Hide file tree
Showing 218 changed files with 52,350 additions and 16 deletions.
39 changes: 35 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,50 @@

import PackageDescription

#if os(Linux)
let excludePortDir = "Sources/agentcore/ibmras/common/port/osx"
#else
let excludePortDir = "Sources/agentcore/ibmras/common/port/linux"
#endif

let package = Package(
name: "SwiftMetrics",
targets: [
Target(name: "SwiftMetrics", dependencies: [.Target(name: "agentcore"),
.Target(name: "cpuplugin"),
.Target(name: "envplugin"),
.Target(name: "memplugin"),
.Target(name: "hcapiplugin")]),
Target(name: "SwiftMetricsKitura", dependencies: ["SwiftMetrics"]),
Target(name: "SwiftMetricsBluemix", dependencies: ["SwiftMetricsKitura"]),
Target(name: "SwiftMetricsDash", dependencies: ["SwiftMetricsBluemix"])
],
Target(name: "SwiftMetricsDash", dependencies: ["SwiftMetricsBluemix"]),
Target(name: "mqttplugin", dependencies: [.Target(name: "paho"),
.Target(name: "agentcore")]),
Target(name: "cpuplugin", dependencies: [.Target(name: "agentcore")]),
Target(name: "envplugin", dependencies: [.Target(name: "agentcore")]),
Target(name: "memplugin", dependencies: [.Target(name: "agentcore")]),
Target(name: "hcapiplugin", dependencies: [.Target(name: "agentcore")])
],
dependencies: [
.Package(url: "https://github.com/RuntimeTools/omr-agentcore.git", majorVersion: 3),
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 6),
.Package(url: "https://github.com/IBM-Swift/Kitura-WebSocket.git", majorVersion: 0, minor: 7),
.Package(url: "https://github.com/IBM-Swift/Kitura-Request.git", majorVersion: 0, minor: 7),
.Package(url: "https://github.com/IBM-Swift/CloudConfiguration.git", majorVersion: 1)
]
],
exclude: [ "Sources/agentcore/ibmras/common/port/aix",
"Sources/agentcore/ibmras/common/port/windows",
"Sources/agentcore/ibmras/common/data",
"Sources/agentcore/ibmras/common/util/memUtils.cpp",
"Sources/ostreamplugin",
"Sources/paho/Windows Build",
"Sources/paho/build",
"Sources/paho/doc",
"Sources/paho/test",
"Sources/paho/src/MQTTClient.c",
"Sources/paho/src/MQTTVersion.c",
"Sources/paho/src/SSLSocket.c",
"Sources/paho/src/samples",
excludePortDir
]
)

65 changes: 65 additions & 0 deletions Package@swift-3.1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright IBM Corporation 2017
*
* Licensed 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
*
* http://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.
**/

import PackageDescription

#if os(Linux)
let excludePortDir = "Sources/agentcore/ibmras/common/port/osx"
#else
let excludePortDir = "Sources/agentcore/ibmras/common/port/linux"
#endif

let package = Package(
name: "SwiftMetrics",
targets: [
Target(name: "SwiftMetrics", dependencies: [.Target(name: "agentcore"),
.Target(name: "cpuplugin"),
.Target(name: "envplugin"),
.Target(name: "memplugin"),
.Target(name: "hcapiplugin")]),
Target(name: "SwiftMetricsKitura", dependencies: ["SwiftMetrics"]),
Target(name: "SwiftMetricsBluemix", dependencies: ["SwiftMetricsKitura"]),
Target(name: "SwiftMetricsDash", dependencies: ["SwiftMetricsBluemix"]),
Target(name: "mqttplugin", dependencies: [.Target(name: "paho"),
.Target(name: "agentcore")]),
Target(name: "cpuplugin", dependencies: [.Target(name: "agentcore")]),
Target(name: "envplugin", dependencies: [.Target(name: "agentcore")]),
Target(name: "memplugin", dependencies: [.Target(name: "agentcore")]),
Target(name: "hcapiplugin", dependencies: [.Target(name: "agentcore")])
],
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 7),
.Package(url: "https://github.com/IBM-Swift/Kitura-WebSocket.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/IBM-Swift/Kitura-Request.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/IBM-Swift/CloudConfiguration.git", majorVersion: 2)
],
exclude: [ "Sources/agentcore/ibmras/common/port/aix",
"Sources/agentcore/ibmras/common/port/windows",
"Sources/agentcore/ibmras/common/data",
"Sources/agentcore/ibmras/common/util/memUtils.cpp",
"Sources/ostreamplugin",
"Sources/paho/Windows Build",
"Sources/paho/build",
"Sources/paho/doc",
"Sources/paho/test",
"Sources/paho/src/MQTTClient.c",
"Sources/paho/src/MQTTVersion.c",
"Sources/paho/src/SSLSocket.c",
"Sources/paho/src/samples",
excludePortDir
]
)

34 changes: 25 additions & 9 deletions Sources/SwiftMetrics/SwiftMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ open class SwiftMetrics {
var latencyEnabled: Bool = true
let jobsQueue = DispatchQueue(label: "Swift Metrics Jobs Queue")

public init() throws{

public init() throws {
self.loaderApi = loader_entrypoint().pointee
try self.loadProperties()
loaderApi.setLogLevels()
Expand Down Expand Up @@ -144,7 +143,7 @@ open class SwiftMetrics {
let fm = FileManager.default
var propertiesPath = ""
let currentDir = fm.currentDirectoryPath
var dirContents = try fm.contentsOfDirectory(atPath: currentDir)
let dirContents = try fm.contentsOfDirectory(atPath: currentDir)
for dir in dirContents {

if dir.contains("swiftmetrics.properties") {
Expand All @@ -170,16 +169,33 @@ open class SwiftMetrics {
} else {
packagesPath = workingPath.substring(to: i!.lowerBound)
}
packagesPath.append("Packages")
_ = fm.changeCurrentDirectoryPath(packagesPath)
///omr-agentcore has a version number in it, so search for it
dirContents = try fm.contentsOfDirectory(atPath: fm.currentDirectoryPath)
for dir in dirContents {
if dir.contains("SwiftMetrics") {
// Swift 3.1
let checkoutsPath = packagesPath + ".build/checkouts/"
if fm.fileExists(atPath: checkoutsPath) {
_ = fm.changeCurrentDirectoryPath(checkoutsPath)

} else { // Swift 3.0
packagesPath.append("Packages/");
if fm.fileExists(atPath: packagesPath) {
_ = fm.changeCurrentDirectoryPath(packagesPath)
} else {
print("SwiftMetrics: Error finding install directory")
}
}

do {
let dirContents = try fm.contentsOfDirectory(atPath: fm.currentDirectoryPath)
for dir in dirContents {
if dir.contains("SwiftMetrics") {
///that's where we want to be!
_ = fm.changeCurrentDirectoryPath(dir)
}
}
} catch {
print("SwiftMetrics: Error searching directory: \(packagesPath), \(error).")
throw error
}

propertiesPath = "\(fm.currentDirectoryPath)/swiftmetrics.properties"
_ = fm.changeCurrentDirectoryPath(currentDir)

Expand Down
22 changes: 19 additions & 3 deletions Sources/SwiftMetricsDash/SwiftMetricsDash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,29 @@ public class SwiftMetricsDash {
} else {
packagesPath = workingPath.substring(to: i!.lowerBound)
}
packagesPath.append("Packages/")
let dirContents = try fm.contentsOfDirectory(atPath: packagesPath)
for dir in dirContents {

// Swift 3.1
let checkoutsPath = packagesPath + ".build/checkouts/"
if fm.fileExists(atPath: checkoutsPath) {
packagesPath = checkoutsPath;
} else if fm.fileExists(atPath: packagesPath + "Packages/") { // Swift 3.0
packagesPath.append("Packages/");
} else {
print("SwiftMetricsDash: error finding install directory")
}

do {
let dirContents = try fm.contentsOfDirectory(atPath: packagesPath)
for dir in dirContents {
if dir.contains("SwiftMetrics") {
packagesPath.append("\(dir)/public")
}
}
} catch {
print("SwiftMetricsDash: Error opening directory: \(packagesPath), \(error).")
throw error
}

router.all("/swiftmetrics-dash", middleware: StaticFileServer(path: packagesPath))

if createServer {
Expand Down
150 changes: 150 additions & 0 deletions Sources/agentcore/ibmras/common/LogManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*******************************************************************************
* Copyright 2016 IBM Corp.
*
* Licensed 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
*
* http://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.
*******************************************************************************/


#include <iostream>
#include <cstdarg>
#include <cstdio>
#include <string>

#include "LogManager.h"


#if defined(_WINDOWS)
#define VPRINT vsprintf_s
#else
#define VPRINT vsprintf
#endif

extern "C" {

DECL void* ibmras_common_LogManager_getLogger(const char* name) {
return (void*) ibmras::common::LogManager::getLogger(name);
}
}

namespace ibmras {
namespace common {

LogManager* LogManager::instance = NULL;

LOCAL_LOGGER_CALLBACK LogManager::localLogFunc = NULL;

LogManager::LogManager() :
level(info), localLog(true), lock(NULL) {
/* do not create a lock in the constructor as it will create a loop with the logging in the port library */
}


void LogManager::processMsg(const std::string &msg) {

if (localLog) {
/* local logging is overriding */
if (localLogFunc) {
localLogFunc(msg);
} else {
std::cerr << msg << '\n';
std::cerr.flush();
}
return;
}

}

void LogManager::msgHandler(const std::string &message, loggingLevel level,
Logger* logger) {

/* logger level has priority over log manager level (which should be considered a default level */
if ((logger->level >= level) || (instance->level >= level)) {
instance->processMsg(message);
}
}

void LogManager::setLevel(loggingLevel newlevel) {
LogManager::level = newlevel;
for (std::vector<Logger*>::iterator i = loggers.begin(); i != loggers.end();
++i) {
if ((*i)->level <= level) {
(*i)->level = level;
}
}
}

void LogManager::setLevel(const std::string &name, loggingLevel newlevel) {
if (name.compare("level") == 0) {
setLevel(newlevel);
} else {
Logger* logger = getLogger(name);
if (level > newlevel) {
logger->level = level;
} else {
logger->level = newlevel;
}
logger->debugLevel = newlevel;
}
}

LogManager* LogManager::getInstance() {
if (!instance) {
instance = new LogManager;
instance->lock = new ibmras::common::port::Lock;
}
return instance;
}

Logger* LogManager::getLogger(const std::string &name) {
LogManager* instance = getInstance();
Logger* logger = instance->findLogger(name);
if (!logger) { /* logger not found so need to create a new instance and return that */
logger = new Logger(name, LogManager::msgHandler);
instance->loggers.push_back(logger);
}

return logger;
}

void LogManager::setLevel(const std::string& name, const std::string& value) {
loggingLevel lev = none;
if (value.compare("warning") == 0) {
lev = warning;
} else if (value.compare("info") == 0) {
lev = info;
} else if (value.compare("fine") == 0) {
lev = fine;
} else if (value.compare("finest") == 0) {
lev = finest;
} else if (value.compare("debug") == 0) {
lev = debug;
} else {
lev = none;
}
setLevel(name, lev);
}

Logger* LogManager::findLogger(const std::string &name) {

for (std::vector<Logger*>::iterator i = loggers.begin(); i != loggers.end();
++i) {
if ((*i)->component == name) {
return (*i);
}
}
return NULL; /* no match found */
}


}
}
Loading

0 comments on commit 299065a

Please sign in to comment.