Skip to content

Commit

Permalink
rename with MP prefix to avoid namespace clashes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmixpanel committed Jan 30, 2025
1 parent c48c94e commit 3485742
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 112 deletions.
46 changes: 23 additions & 23 deletions MixpanelDemo/MixpanelDemoTests/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,63 @@ class LoggerTests: XCTestCase {

func testEnableDebug() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.debug)
MPLogger.addLogging(counter)
MPLogger.enableLevel(.debug)

Logger.debug(message: "logged")
MPLogger.debug(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testEnableInfo() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.info)
Logger.info(message: "logged")
MPLogger.addLogging(counter)
MPLogger.enableLevel(.info)
MPLogger.info(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testEnableWarning() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.warning)
Logger.warn(message: "logged")
MPLogger.addLogging(counter)
MPLogger.enableLevel(.warning)
MPLogger.warn(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testEnableError() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.error)
Logger.error(message: "logged")
MPLogger.addLogging(counter)
MPLogger.enableLevel(.error)
MPLogger.error(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testDisabledLogging() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.disableLevel(.debug)
Logger.debug(message: "not logged")
MPLogger.addLogging(counter)
MPLogger.disableLevel(.debug)
MPLogger.debug(message: "not logged")
XCTAssertEqual(0, counter.count)

Logger.disableLevel(.error)
Logger.error(message: "not logged")
MPLogger.disableLevel(.error)
MPLogger.error(message: "not logged")
XCTAssertEqual(0, counter.count)

Logger.disableLevel(.info)
Logger.info(message: "not logged")
MPLogger.disableLevel(.info)
MPLogger.info(message: "not logged")
XCTAssertEqual(0, counter.count)

Logger.disableLevel(.warning)
Logger.warn(message: "not logged")
MPLogger.disableLevel(.warning)
MPLogger.warn(message: "not logged")
XCTAssertEqual(0, counter.count)
}
}

/// This is a stub that implements `Logging` to be passed to our `Logger` instance for testing
class CounterLogging: Logging {
class CounterLogging: MPLogging {
var count = 0

func addMessage(message: LogMessage) {
func addMessage(message: MPLogMessage) {
count = count + 1
}
}
2 changes: 1 addition & 1 deletion Sources/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ErrorHandler {

class func logError(_ error: Error) {
let stackSymbols = Thread.callStackSymbols
Logger.error(message: "Error: \(error) \n Stack Symbols: \(stackSymbols)")
MPLogger.error(message: "Error: \(error) \n Stack Symbols: \(stackSymbols)")
}

}
4 changes: 2 additions & 2 deletions Sources/FileLogging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// Logs all messages to a file
class FileLogging: Logging {
class FileLogging: MPLogging {
private let fileHandle: FileHandle

init(path: String) {
Expand All @@ -28,7 +28,7 @@ class FileLogging: Logging {
fileHandle.closeFile()
}

func addMessage(message: LogMessage) {
func addMessage(message: MPLogMessage) {
let string = "File: \(message.file) - Func: \(message.function) - " +
"Level: \(message.level.rawValue) - Message: \(message.text)"
if let data = string.data(using: String.Encoding.utf8) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Flush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class Flush: AppLifecycle {
(entity["id"] as? Int32) ?? 0
}
// Log data payload sent
Logger.debug(message: "Sending batch of data")
Logger.debug(message: batch as Any)
MPLogger.debug(message: "Sending batch of data")
MPLogger.debug(message: batch as Any)
let requestData = JSONHandler.encodeAPIData(batch)
if let requestData = requestData {
#if os(iOS)
Expand Down
4 changes: 2 additions & 2 deletions Sources/FlushRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ class FlushRequest: Network {
failure: { (reason, _, response) in
self.networkConsecutiveFailures += 1
self.updateRetryDelay(response)
Logger.warn(message: "API request to \(resource.path) has failed with reason \(reason)")
MPLogger.warn(message: "API request to \(resource.path) has failed with reason \(reason)")
completion(false)
}, success: { (result, response) in
self.networkConsecutiveFailures = 0
self.updateRetryDelay(response)
if result == 0 {
Logger.info(message: "\(base) api rejected some items")
MPLogger.info(message: "\(base) api rejected some items")
}
completion(true)
})
Expand Down
10 changes: 5 additions & 5 deletions Sources/JSONHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class JSONHandler {
let data: Data? = serializeJSONObject(obj)

guard let d = data else {
Logger.warn(message: "couldn't serialize object")
MPLogger.warn(message: "couldn't serialize object")
return nil
}

Expand All @@ -28,7 +28,7 @@ class JSONHandler {
do {
object = try JSONSerialization.jsonObject(with: data, options: [])
} catch {
Logger.warn(message: "exception decoding object data")
MPLogger.warn(message: "exception decoding object data")
}
return object
}
Expand All @@ -44,7 +44,7 @@ class JSONHandler {
}

guard JSONSerialization.isValidJSONObject(serializableJSONObject) else {
Logger.warn(message: "object isn't valid and can't be serialzed to JSON")
MPLogger.warn(message: "object isn't valid and can't be serialzed to JSON")
return nil
}

Expand All @@ -53,7 +53,7 @@ class JSONHandler {
serializedObject = try JSONSerialization
.data(withJSONObject: serializableJSONObject, options: [])
} catch {
Logger.warn(message: "exception encoding api data")
MPLogger.warn(message: "exception encoding api data")
}
return serializedObject
}
Expand Down Expand Up @@ -110,7 +110,7 @@ class JSONHandler {
// all nil properties outside of Arrays are converted to NSNull()
return NSNull()
} else {
Logger.info(message: "enforcing string on object")
MPLogger.info(message: "enforcing string on object")
return objString
}
}
Expand Down
30 changes: 15 additions & 15 deletions Sources/MPDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,28 @@ class MPDB {
}

private func reconnect() {
Logger.warn(message: "No database connection found. Calling MPDB.open()")
MPLogger.warn(message: "No database connection found. Calling MPDB.open()")
open()
}

func open() {
if apiToken.isEmpty {
Logger.error(message: "Project token must not be empty. Database cannot be opened.")
MPLogger.error(message: "Project token must not be empty. Database cannot be opened.")
return
}
if let dbPath = pathToDb() {
if sqlite3_open_v2(dbPath, &connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) != SQLITE_OK {
logSqlError(message: "Error opening or creating database at path: \(dbPath)")
close()
} else {
Logger.info(message: "Successfully opened connection to database at path: \(dbPath)")
MPLogger.info(message: "Successfully opened connection to database at path: \(dbPath)")
if let db = connection {
let pragmaString = "PRAGMA journal_mode=WAL;"
var pragmaStatement: OpaquePointer?
if sqlite3_prepare_v2(db, pragmaString, -1, &pragmaStatement, nil) == SQLITE_OK {
if sqlite3_step(pragmaStatement) == SQLITE_ROW {
let res = String(cString: sqlite3_column_text(pragmaStatement, 0))
Logger.info(message: "SQLite journal mode set to \(res)")
MPLogger.info(message: "SQLite journal mode set to \(res)")
} else {
logSqlError(message: "Failed to enable journal_mode=WAL")
}
Expand All @@ -85,7 +85,7 @@ class MPDB {
func close() {
sqlite3_close(connection)
connection = nil
Logger.info(message: "Connection to database closed.")
MPLogger.info(message: "Connection to database closed.")
}

private func recreate() {
Expand All @@ -95,10 +95,10 @@ class MPDB {
let manager = FileManager.default
if manager.fileExists(atPath: dbPath) {
try manager.removeItem(atPath: dbPath)
Logger.info(message: "Deleted database file at path: \(dbPath)")
MPLogger.info(message: "Deleted database file at path: \(dbPath)")
}
} catch let error {
Logger.error(message: "Unable to remove database file at path: \(dbPath), error: \(error)")
MPLogger.error(message: "Unable to remove database file at path: \(dbPath), error: \(error)")
}
}
reconnect()
Expand All @@ -112,7 +112,7 @@ class MPDB {
var createTableStatement: OpaquePointer?
if sqlite3_prepare_v2(db, createTableString, -1, &createTableStatement, nil) == SQLITE_OK {
if sqlite3_step(createTableStatement) == SQLITE_DONE {
Logger.info(message: "\(tableName) table created")
MPLogger.info(message: "\(tableName) table created")
} else {
logSqlError(message: "\(tableName) table create failed")
}
Expand All @@ -133,7 +133,7 @@ class MPDB {
var createIndexStatement: OpaquePointer?
if sqlite3_prepare_v2(db, createIndexString, -1, &createIndexStatement, nil) == SQLITE_OK {
if sqlite3_step(createIndexStatement) == SQLITE_DONE {
Logger.info(message: "\(indexName) index created")
MPLogger.info(message: "\(indexName) index created")
} else {
logSqlError(message: "\(indexName) index creation failed")
}
Expand Down Expand Up @@ -167,7 +167,7 @@ class MPDB {
sqlite3_bind_int(insertStatement, 2, flag ? 1 : 0)
sqlite3_bind_double(insertStatement, 3, Date().timeIntervalSince1970)
if sqlite3_step(insertStatement) == SQLITE_DONE {
Logger.info(message: "Successfully inserted row into table \(tableName)")
MPLogger.info(message: "Successfully inserted row into table \(tableName)")
} else {
logSqlError(message: "Failed to insert row into table \(tableName)")
recreate()
Expand All @@ -191,7 +191,7 @@ class MPDB {
var deleteStatement: OpaquePointer?
if sqlite3_prepare_v2(db, deleteString, -1, &deleteStatement, nil) == SQLITE_OK {
if sqlite3_step(deleteStatement) == SQLITE_DONE {
Logger.info(message: "Successfully deleted rows from table \(tableName)")
MPLogger.info(message: "Successfully deleted rows from table \(tableName)")
} else {
logSqlError(message: "Failed to delete rows from table \(tableName)")
recreate()
Expand Down Expand Up @@ -223,7 +223,7 @@ class MPDB {
var updateStatement: OpaquePointer?
if sqlite3_prepare_v2(db, updateString, -1, &updateStatement, nil) == SQLITE_OK {
if sqlite3_step(updateStatement) == SQLITE_DONE {
Logger.info(message: "Successfully updated rows from table \(tableName)")
MPLogger.info(message: "Successfully updated rows from table \(tableName)")
} else {
logSqlError(message: "Failed to update rows from table \(tableName)")
recreate()
Expand Down Expand Up @@ -266,7 +266,7 @@ class MPDB {
}
}
if rowsRead > 0 {
Logger.info(message: "Successfully read \(rowsRead) from table \(tableName)")
MPLogger.info(message: "Successfully read \(rowsRead) from table \(tableName)")
}
} else {
logSqlError(message: "SELECT statement for table \(tableName) could not be prepared")
Expand All @@ -281,10 +281,10 @@ class MPDB {
private func logSqlError(message: String? = nil) {
if let db = connection {
if let msg = message {
Logger.error(message: msg)
MPLogger.error(message: msg)
}
let sqlError = String(cString: sqlite3_errmsg(db)!)
Logger.error(message: sqlError)
MPLogger.error(message: sqlError)
} else {
reconnect()
}
Expand Down
Loading

0 comments on commit 3485742

Please sign in to comment.